Skip to content

Instantly share code, notes, and snippets.

View splincode's full-sized avatar
💬
печатает...

Максим Иванов splincode

💬
печатает...
View GitHub Profile
// npm install tslint-immutable --save-dev
// tslint.json
{
"extends": ["tslint-immutable"],
"rules": {
// Recommended built-in rules
"no-var-keyword": true,
"no-parameter-reassignment": true,
"typedef": [true, "call-signature"],
const obj = {
a: 1,
b: 2,
c: {
d: 3
}
};
obj.newField = 5; // You can assign new field
export type Immutable<T> = T extends (infer R)[]
? ImmutableArray<R>
: T extends Function
? T
: T extends object
? ImmutableObject<T>
: T;
interface ImmutableArray<T> extends ReadonlyArray<Immutable<T>> {}
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone
}
const animals = {
zebra: {
food: ['leaves', 'bark'],
name: 'zebra'
},
panda: {
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone
}
const original = {
a: undefined,
b: { c: undefined },
d: () => undefined
};
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone
}
const original = {
a: new Date(),
b: NaN,
c: new Function(),
d: undefined,
e: function(){},
function deepCopy(obj) {
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone
}
function copy(obj) {
let clone = Array.isArray(obj) ? [] : {};
for (let key in obj) {
let value = obj[key];
clone[key] = (typeof value === "object") ? copy(value) : value;
@Component({
selector: 'my-app',
template: `
<b>default</b>: {{ todos$ | async | json }} <br>
<b>immutable reverse</b>: {{ todosReverse$ | async | json }}
`
})
export class AppComponent implements OnInit {
@Select(AppState)
import { State, Selector, StateContext, Action } from '@ngxs/store';
import { ImmutableSelector, ImmutableContext } from '@ngxs-labs/immer-adapter';
export class Add {
static type = 'Add'
constructor(public payload: number) {}
}
@State<number[]>({
name: 'todos',
@State<AnimalsStateModel>({
name: 'animals',
defaults: {
zebra: {
food: ['leaves', 'bark'],
name: 'zebra'
},
panda: {
food: [],
name: 'panda'