This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const obj = { | |
a: 1, | |
b: 2, | |
c: { | |
d: 3 | |
} | |
}; | |
obj.newField = 5; // You can assign new field |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>> {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepCopy(obj) { | |
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone | |
} | |
const animals = { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deepCopy(obj) { | |
return JSON.parse(JSON.stringify(obj)); // serialize -> deserialize -> unique clone | |
} | |
const original = { | |
a: undefined, | |
b: { c: undefined }, | |
d: () => undefined | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@State<AnimalsStateModel>({ | |
name: 'animals', | |
defaults: { | |
zebra: { | |
food: ['leaves', 'bark'], | |
name: 'zebra' | |
}, | |
panda: { | |
food: [], | |
name: 'panda' |