Skip to content

Instantly share code, notes, and snippets.

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

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

💬
печатает...
View GitHub Profile
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 animals = {
zebra: {
food: ['leaves', 'bark'],
name: 'zebra'
},
panda: {
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>> {}
const obj = {
a: 1,
b: 2,
c: {
d: 3
}
};
obj.newField = 5; // You can assign new field
// 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"],
/*
INCORRECT CHANGES (deep mutation)
*/
const animals = {
zebra: {
food: ['leaves', 'bark'],
name: 'zebra'
},
panda: {
const animals = {
zebra: {
food: ['leaves', 'bark'],
name: 'zebra'
},
panda: {
food: [],
name: 'panda'
}
};
@NgModule({
imports: [
BrowserModule,
NgxsModule.forRoot([ AppState ], { developmentMode: !env.production })
],
...
})
export class AppModule { }
<b>default</b>: {{ todos$ | async | json }} <br>
<b>immutable reverse</b>: {{ immutableTodos$ | async | json }} <br>
<b>mutable reverse</b>: {{ mutableTodos$ | async | json }} <br>
@State<number[]>({
name: 'todos',
defaults: [1, 2, 3]
})
export class AppState {
@Selector() public static mutableTodos(state: number[]): number[] {
return state.reverse();
}