diff --git a/types/icepick/icepick-tests.ts b/types/icepick/icepick-tests.ts index f1d536e78f..150707756a 100644 --- a/types/icepick/icepick-tests.ts +++ b/types/icepick/icepick-tests.ts @@ -1,15 +1,15 @@ -import i = require("icepick"); +import * as i from 'icepick'; -"use strict"; // so attempted modifications of frozen objects will throw errors +'use strict'; // so attempted modifications of frozen objects will throw errors // freeze(collection) { - let coll = { - a: "foo", + const coll = { + a: 'foo', b: [1, 2, 3], c: { - d: "bar" - } + d: 'bar', + }, }; i.freeze(coll); @@ -19,157 +19,212 @@ import i = require("icepick"); class Foo {} { - let coll = i.freeze({ a: "foo", b: [1, 2, 3], c: { d: "bar" }, e: new Foo() }); - let thawed = i.thaw(coll); + const coll = i.freeze({ a: 'foo', b: [1, 2, 3], c: { d: 'bar' }, e: new Foo() }); + const thawed = i.thaw(coll); } // assoc(collection, key, value) { - let coll = { a: 1, b: 2 }; - let newColl = i.assoc(coll, "b", 3); // {a: 1, b: 3} + const coll = { a: 1, b: 2 }; + const newColl = i.assoc(coll, 'b', 3); // {a: 1, b: 3} - let arr = ["a", "b", "c"]; - let newArr = i.assoc(arr, 2, "d"); // ["a", "b", "d"] + const arr = ['a', 'b', 'c']; + const newArr = i.assoc(arr, 2, 'd'); // ["a", "b", "d"] } // alias: set(collection, key, value) { - let coll = { a: 1, b: 2 }; - let newColl = i.set(coll, "b", 3); // {a: 1, b: 3} + const coll = { a: 1, b: 2 }; + const newColl = i.set(coll, 'b', 3); // {a: 1, b: 3} - let arr = ["a", "b", "c"]; - let newArr = i.set(arr, 2, "d"); // ["a", "b", "d"] + const arr = ['a', 'b', 'c']; + const newArr = i.set(arr, 2, 'd'); // ["a", "b", "d"] } // dissoc(collection, key) { - let coll = { a: 1, b: 2, c: 3 }; - let newColl = i.dissoc(coll, "b"); // {a: 1, c: 3} + const coll = { a: 1, b: 2, c: 3 }; + const newColl = i.dissoc(coll, 'b'); // {a: 1, c: 3} - let arr = ["a", "b", "c"]; - let newArr = i.dissoc(arr, 2); // ["a", , "c"] + const arr = ['a', 'b', 'c']; + const newArr = i.dissoc(arr, 2); // ["a", , "c"] } // alias: unset(collection, key) { - let coll = { a: 1, b: 2, c: 3 }; - let newColl = i.unset(coll, "b"); // {a: 1, c: 3} + const coll = { a: 1, b: 2, c: 3 }; + const newColl = i.unset(coll, 'b'); // {a: 1, c: 3} - let arr = ["a", "b", "c"]; - let newArr = i.unset(arr, 2); // ["a", , "c"] + const arr = ['a', 'b', 'c']; + const newArr = i.unset(arr, 2); // ["a", , "c"] +} + +// dissocIn(collection, path) +{ + const coll = { a: 1, b: { d: 5, e: 7 }, c: 3 }; + const newColl = i.dissocIn(coll, ['b', 'd']); // {a: 1, {b: {e: 7}}, c: 3} + + const col2 = { a: 1, b: { d: 5 }, c: 3 }; + const newCol2 = i.dissocIn(coll, ['b', 'd']); // {a: 1, {b: {}}, c: 3} +} + +// alias: unsetIn(collection, path) +{ + const coll = { a: 1, b: { d: 5, e: 7 }, c: 3 }; + const newColl = i.unsetIn(coll, ['b', 'd']); // {a: 1, {b: {e: 7}}, c: 3} + + const col2 = { a: 1, b: { d: 5 }, c: 3 }; + const newCol2 = i.unsetIn(coll, ['b', 'd']); // {a: 1, {b: {}}, c: 3} } // assocIn(collection, path, value) { - let coll = { - a: "foo", + const coll = { + a: 'foo', b: [1, 2, 3], c: { - d: "bar" - } + d: 'bar', + }, }; - let newColl = i.assocIn(coll, ["c", "d"], "baz"); + const newColl = i.assocIn(coll, ['c', 'd'], 'baz'); - let coll2 = {}; - let newColl2 = i.assocIn(coll2, ["a", "b", "c"], 1); + const coll2 = {}; + const newColl2 = i.assocIn(coll2, ['a', 'b', 'c'], 1); } // alias: setIn(collection, path, value) { - let coll = { - a: "foo", + const coll = { + a: 'foo', b: [1, 2, 3], c: { - d: "bar" - } + d: 'bar', + }, }; - let newColl = i.setIn(coll, ["c", "d"], "baz"); + const newColl = i.setIn(coll, ['c', 'd'], 'baz'); - let coll2 = {}; - let newColl2 = i.setIn(coll2, ["a", "b", "c"], 1); + const coll2 = {}; + const newColl2 = i.setIn(coll2, ['a', 'b', 'c'], 1); } // getIn(collection, path) { - let coll = i.freeze([ - { a: 1 }, - { b: 2 } - ]); + const coll = i.freeze([{ a: 1 }, { b: 2 }]); - let result = i.getIn(coll, [1, "b"]) as number; // 2 + const result = i.getIn(coll, [1, 'b']) as number; // 2 } // updateIn(collection, path, callback) { - let coll = i.freeze([ - { a: 1 }, - { b: 2 } - ]); + const coll = i.freeze([{ a: 1 }, { b: 2 }]); - let newColl = i.updateIn(coll, [1, "b"], function(val: number) { - return val * 2; - }); // [ {a: 1}, {b: 4} ] + const newColl = i.updateIn(coll, [1, 'b'], (val: number) => val * 2); // [ {a: 1}, {b: 4} ] } // assign(coll1, coll2, ...) { - let obj1 = { a: 1, b: 2, c: 3 }; - let obj2 = { c: 4, d: 5 }; + const obj1 = { a: 1, b: 2, c: 3 }; + const obj2 = { c: 4, d: 5 }; - let result = i.assign(obj1, obj2); // {a: 1, b: 2, c: 4, d: 5} + const result = i.assign(obj1, obj2); // {a: 1, b: 2, c: 4, d: 5} +} + +// alias: extend(coll1, coll2, ...) +{ + const obj1 = { a: 1, b: 2, c: 3 }; + const obj2 = { c: 4, d: 5 }; + + const result = i.extend(obj1, obj2); // {a: 1, b: 2, c: 4, d: 5} } // merge(target, source) { - let defaults = { a: 1, c: { d: 1, e: [1, 2, 3], f: { g: 1 } } }; - let obj = { c: { d: 2, e: [2], f: null as any } }; + const defaults = { a: 1, c: { d: 1, e: [1, 2, 3], f: { g: 1 } } }; + const obj = { c: { d: 2, e: [2], f: null as any } }; - let result1 = i.merge(defaults, obj); // {a: 1, c: {d: 2, e: [2]}, f: null} + const result1 = i.merge(defaults, obj); // {a: 1, c: {d: 2, e: [2]}, f: null} - let obj2 = { c: { d: 2 } }; - let result2 = i.merge(result1, obj2); + const obj2 = { c: { d: 2 } }; + const result2 = i.merge(result1, obj2); - (result1 === result2); // true + result1 === result2; // true } // arrays { - var a = [1]; + let a = [1]; a = i.push(a, 2); // [1, 2]; a = i.unshift(a, 0); // [0, 1, 2]; a = i.pop(a); // [0, 1]; a = i.shift(a); // [1]; + a = i.splice(a, 0, 0, 2, 3); // [2, 3, 1] + a = i.slice(a, 2, 1); // [1] } { - i.map(function(v) { return v * 2 }, [1, 2, 3]); // [2, 4, 6] + i.map(v => v * 2, [1, 2, 3]); // [2, 4, 6] - i.filter(function(v: number) { return v % 2 === 0; }, [1, 2, 3]); // [1, 3] + i.filter((v: number) => v % 2 === 0, [1, 2, 3]); // [1, 3] } { - var arr = i.freeze([{ a: 1 }, { b: 2 }]); + const arr = i.freeze([{ a: 1 }, { b: 2 }]); - //ECMAScript 2015 - //arr.find(function(item) { return item.b != null; }); // {b: 2} + // ECMAScript 2015 + // arr.find(function(item) { return item.b != null; }); // {b: 2} } // chain(coll) - not defined { - let o = { + const o = { a: [1, 2, 3], b: { c: 1 }, - d: 4 + d: 4, }; - let result = i.chain(o) - .assocIn(["a", 2], 4) - .setIn(["a", 1], 5) - .updateIn(["d"], function(d) { return d * 2 }) + const result = i + .chain(o) + .assocIn(['a', 2], 4) + .setIn(['a', 1], 5) + .updateIn(['d'], d => d * 2) .merge({ b: { c: 2, c2: 3 } }) - .assoc("e", 2) - .set("f", 3) - .dissoc("d") + .assoc('e', 2) + .set('f', 3) + .dissoc('d') .getIn(['a', 0]) .value() as number; } + +// readonly array +{ + // typescript@3.3 + const a: ReadonlyArray = [1]; + + // typescript@3.4 + // const a: readonly number[] = [1]; + + let result: number[]; + result = i.push(a, 2); + result = i.unshift(a, 0); + result = i.pop(a); + result = i.shift(a); + result = i.splice(a, 0, 0, 2); + result = i.slice(a, 1, 1); + result = i.map(x => x, a); + result = i.filter(x => x > 1, a); +} +{ + // typescript@3.3 + const a: Readonly<[number, string]> = [1, 'one']; + + // typescript@3.4 + // const a: readonly [number, string] = [1, "one"]; + + const result = i.set(a, 1, 'two'); +} + +// readonly object +{ + const obj1: Readonly<{ readonly a: number; b: number; c: number }> = { a: 1, b: 2, c: 3 }; + const result = i.assocIn(obj1, ['a'], 2); +} diff --git a/types/icepick/index.d.ts b/types/icepick/index.d.ts index fe7a3d0b36..ef409e9321 100644 --- a/types/icepick/index.d.ts +++ b/types/icepick/index.d.ts @@ -1,44 +1,47 @@ -// Type definitions for icepick v1.3.0 +// Type definitions for icepick 2.3 // Project: https://github.com/aearly/icepick // Definitions by: Nathan Brown , Tobias Cohen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +export function freeze(collection: T): T; +export function thaw(collection: T): T; +export function assoc(collection: T, key: number | string, value: any): T; +export function dissoc(collection: T, key: number | string): T; +export function dissocIn(collection: T, path: ReadonlyArray): T; +export function assocIn(collection: T, path: ReadonlyArray, value: any): T; +export function getIn(collection: any, path: ReadonlyArray): any; +export function updateIn( + collection: T, + path: ReadonlyArray, + callback: (value: any) => any, +): T; -export declare function freeze(collection: T): T; -export declare function thaw(collection: T): T; -export declare function assoc(collection: T, key: number | string, value: V): T; -export declare function dissoc(collection: T, key: number | string): T; -export declare function dissocIn(collection: T, path: Array): T; -export declare function assocIn(collection: T, path: Array, value: V): T; -export declare function getIn(collection: T, path: Array): any; -export declare function updateIn(collection: T, path: Array, callback: (value: V) => V): T; +export { assoc as set }; +export { assocIn as setIn }; +export { dissoc as unset }; +export { dissocIn as unsetIn }; -export {assoc as set}; -export {assocIn as setIn}; -export {dissoc as unset}; -export {dissocIn as unsetIn}; +export function assign(target: T): T; +export function assign(target: T, source1: S1): T & S1; +export function assign(target: T, s1: S1, s2: S2): T & S1 & S2; +export function assign(target: T, s1: S1, s2: S2, s3: S3): T & S1 & S2 & S3; +export function assign(target: T, s1: S1, s2: S2, s3: S3, s4: S4): T & S1 & S2 & S3 & S4; -export declare function assign(target: T): T; -export declare function assign(target: T, source1: S1): (T & S1); -export declare function assign(target: T, s1: S1, s2: S2): (T & S1 & S2); -export declare function assign(target: T, s1: S1, s2: S2, s3: S3): (T & S1 & S2 & S3); -export declare function assign(target: T, s1: S1, s2: S2, s3: S3, s4: S4): (T & S1 & S2 & S3 & S4); +export { assign as extend }; -export {assign as extend}; +export function merge(target: T, source: S1): T & S1; -export declare function merge(target: T, source: S1): (T & S1); +export function push(array: ReadonlyArray, element: T): T[]; +export function pop(array: ReadonlyArray): T[]; +export function shift(array: ReadonlyArray): T[]; +export function unshift(array: ReadonlyArray, element: T): T[]; +export function reverse(array: ReadonlyArray): T[]; +export function sort(array: ReadonlyArray, compareFunction?: (a: T, b: T) => number): T[]; +export function splice(array: ReadonlyArray, start: number, deleteCount: number, ...items: T[]): T[]; +export function slice(array: ReadonlyArray, begin?: number, end?: number): T[]; -export declare function push(array: T[], element: T): T[]; -export declare function pop(array: T[]): T[]; -export declare function shift(array: T[]): T[]; -export declare function unshift(array: T[], element: T): T[]; -export declare function reverse(array: T[]): T[]; -export declare function sort(array: T[], compareFunction?: (a: T, b: T) => number): T[]; -export declare function splice(array: T[], start: number, deleteCount: number, ...items: T[]): T[]; -export declare function slice(array: T[], begin?: number, end?: number): T[]; - -export declare function map(fn: (value: T) => U, array: T[]): U[]; -export declare function filter(fn: (value: T) => boolean, array: T[]): T[]; +export function map(fn: (value: T) => U, array: ReadonlyArray): U[]; +export function filter(fn: (value: T) => boolean, array: ReadonlyArray): T[]; interface IcepickWrapper { value(): T; @@ -46,20 +49,20 @@ interface IcepickWrapper { freeze(): IcepickWrapper; thaw(): IcepickWrapper; - assoc(key: number | string, value: V): IcepickWrapper; - set(key: number | string, value: V): IcepickWrapper; + assoc(key: number | string, value: any): IcepickWrapper; + set(key: number | string, value: any): IcepickWrapper; dissoc(key: number | string): IcepickWrapper; unset(key: number | string): IcepickWrapper; - assocIn(path: Array, value: V): IcepickWrapper; - setIn(path: Array, value: V): IcepickWrapper; - - dissocIn(path: Array): IcepickWrapper; - unsetIn(path: Array): IcepickWrapper; + assocIn(path: Array, value: any): IcepickWrapper; + setIn(path: Array, value: any): IcepickWrapper; + + dissocIn(path: Array): IcepickWrapper; + unsetIn(path: Array): IcepickWrapper; getIn(path: Array): IcepickWrapper; - updateIn(path: Array, callback: (value: V) => V): IcepickWrapper; + updateIn(path: Array, callback: (value: any) => any): IcepickWrapper; assign(source1: S1): IcepickWrapper; assign(s1: S1, s2: S2): IcepickWrapper; @@ -73,4 +76,4 @@ interface IcepickWrapper { merge(source: S1): IcepickWrapper; } -export declare function chain(target: T): IcepickWrapper; +export function chain(target: T): IcepickWrapper; diff --git a/types/icepick/tsconfig.json b/types/icepick/tsconfig.json index 530031866b..20c00a53f7 100644 --- a/types/icepick/tsconfig.json +++ b/types/icepick/tsconfig.json @@ -2,13 +2,12 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es6", - "dom" + "es6" ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, - "strictFunctionTypes": false, + "strictNullChecks": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/icepick/tslint.json b/types/icepick/tslint.json index 3d59f55fda..10d875b8db 100644 --- a/types/icepick/tslint.json +++ b/types/icepick/tslint.json @@ -1,80 +1,4 @@ { "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "npm-naming": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } + "rules": {} }