immutability-helper
Advanced tools
Comparing version 2.7.1 to 2.8.0
104
index.d.ts
// Project: Immutability helper | ||
// TypeScript Version: 2.2 | ||
// TypeScript Version: 2.9 | ||
export default update | ||
export default update; | ||
declare function update<T>( | ||
data: ReadonlyArray<T>, | ||
query: ArrayOperators<T>, | ||
): ReadonlyArray<T> | ||
declare function update<T, C extends CustomCommands<object> = never>( | ||
target: T, | ||
spec: Spec<T, C>, | ||
): T; | ||
declare function update<T>( | ||
data: ReadonlySet<T>, | ||
query: SetOperators<T>, | ||
): ReadonlySet<T> | ||
declare namespace update { | ||
function newContext(): typeof update; | ||
function extend<T>( | ||
command: string, | ||
handler: (param: any, old: T) => T, | ||
): void; | ||
} | ||
declare function update<K, V>( | ||
data: ReadonlyMap<K, V>, | ||
query: MapOperators<K, V>, | ||
): ReadonlyMap<K, V> | ||
// Usage with custom commands is as follows: | ||
// | ||
// interface MyCommands { | ||
// $foo: string; | ||
// } | ||
// | ||
// update<Foo, CustomCommands<MyCommands>>(..., { $foo: "bar" }); | ||
// | ||
// It is suggested that if you use custom commands frequently, you wrap and re-export a | ||
// properly-typed version of `update`: | ||
// | ||
// function myUpdate<T>(object: T, spec: Spec<T, CustomCommands<MyCommands>>) { | ||
// return update(object, spec); | ||
// } | ||
// | ||
// See https://github.com/kolodny/immutability-helper/pull/108 for explanation of why this | ||
// type exists. | ||
export type CustomCommands<T> = T & { __noInferenceCustomCommandsBrand: any }; | ||
declare function update<T>(data: T, query: Query<T>): T | ||
export type Spec<T, C extends CustomCommands<object> = never> = | ||
| ( | ||
T extends (Array<infer U> | ReadonlyArray<infer U>) ? ArraySpec<U, C> : | ||
T extends (Map<infer K, infer V> | ReadonlyMap<infer K, infer V>) ? MapSpec<K, V> : | ||
T extends (Set<infer U> | ReadonlySet<infer U>) ? SetSpec<U> : | ||
T extends object ? ObjectSpec<T, C> : | ||
never | ||
) | ||
| { $set: T } | ||
| { $apply: (v: T) => T } | ||
| ((v: T) => T) | ||
| (C extends CustomCommands<infer O> ? O : never); | ||
type Tree<T> = {[K in keyof T]?: Query<T[K]>} | ||
export type Query<T> = | ||
| Tree<T> | ||
| ObjectOperators<T> | ||
| ArrayOperators<any> | ||
| SetOperators<any> | ||
type ArraySpec<T, C extends CustomCommands<object>> = | ||
| { $push: T } | ||
| { $unshift: T } | ||
| { $splice: Array<[number] | [number, number] | [number, number, T]> } | ||
| { [index: string]: Spec<T, C> }; // Note that this does not type check properly if index: number. | ||
type ObjectOperators<T> = | ||
| {$set: any} | ||
| {$toggle: Array<keyof T | number>} | ||
| {$unset: Array<keyof T | number>} | ||
| {$merge: Partial<T>} | ||
| {$apply: (old: T) => T} | ||
| ((old: T) => any) | ||
type ArrayOperators<T> = | ||
| {$push: T} | ||
| {$unshift: T} | ||
| {$splice: Array<[number, number]>} | ||
| {[customCommand: string]: any} | ||
type MapSpec<K, V> = | ||
| { $add: Array<[K, V]> } | ||
| { $remove: K[] }; | ||
type MapOperators<K, V> = {$add: Array<[K, V]>} | {$remove: K[]} | ||
type SetOperators<T> = {$add: T[]} | {$remove: T[]} | ||
type SetSpec<T> = | ||
| { $add: T[] } | ||
| { $remove: T[] }; | ||
declare namespace update { | ||
function newContext(): typeof update | ||
function extend<T>( | ||
command: Command, | ||
handler: (param: CommandArg, old: T) => T, | ||
): void | ||
type Command = string | ||
type CommandArg = any | ||
} | ||
type ObjectSpec<T, C extends CustomCommands<object>> = | ||
| { $toggle: Array<keyof T> } | ||
| { $unset: Array<keyof T> } | ||
| { $merge: Partial<T> } | ||
| { [K in keyof T]?: Spec<T[K], C> }; |
@@ -85,3 +85,7 @@ var invariant = require('invariant'); | ||
: update(object[key], spec[key]); | ||
if (!update.isEquals(nextValueForKey, nextObject[key]) || typeof nextValueForKey === 'undefined' && !hasOwnProperty.call(object, key)) { | ||
var nextObjectValue = | ||
type(nextObject) === 'Map' | ||
? nextObject.get(key) | ||
: nextObject[key]; | ||
if (!update.isEquals(nextValueForKey, nextObjectValue) || typeof nextValueForKey === 'undefined' && !hasOwnProperty.call(object, key)) { | ||
if (nextObject === object) { | ||
@@ -88,0 +92,0 @@ nextObject = copy(object); |
{ | ||
"name": "immutability-helper", | ||
"version": "2.7.1", | ||
"version": "2.8.0", | ||
"description": "mutate a copy of data without changing the original source", | ||
@@ -11,4 +11,4 @@ "main": "index.js", | ||
"test": "mocha test.js", | ||
"test-dtslint": "npm run node-gt4 && dtslint || true", | ||
"node-gt4": "node -e 'process.exit(/v(\\d+)/.exec(process.version)[1] <= 4) ? 1 : 0'" | ||
"test-dtslint": "npm run --silent node-lte4 || dtslint", | ||
"node-lte4": "node -e 'process.exit(+(/v(\\d+)/.exec(process.version)[1]) <= 4 ? 0 : 1)'" | ||
}, | ||
@@ -15,0 +15,0 @@ "keywords": [ |
@@ -580,4 +580,10 @@ var update = require('./'); | ||
); | ||
}) | ||
}); | ||
it('supports Maps and keeps reference equality when possible', function() { | ||
var original = new Map([['a', { b: 1 }]]); | ||
expect(update(original, { a: { $merge: {} } })).toBe(original); | ||
expect(update(original, { a: { $merge: { c: 2 } } })).toNotBe(original); | ||
}); | ||
}); |
@@ -8,4 +8,4 @@ { | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"noEmit": true, | ||
"strictFunctionTypes": false, | ||
@@ -12,0 +12,0 @@ "baseUrl": ".", |
{ | ||
"extends": "dtslint/dtslint.json", | ||
"rules": { | ||
"semicolon": false | ||
} | ||
"extends": "dtslint/dtslint.json", | ||
"rules": { | ||
"no-unnecessary-generics": false, | ||
"file-name-casing": false | ||
} | ||
} | ||
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
89500
15
2416