[icepick] Updates to input arrays to use ReadonlyArray, general cleanup, major version bump (#40807)

* [icepick] Make tsconfig.json more strict

* [icepick] input arrays can be readonly

* [icepick] add tests new alias and methods already in the definitions.

* [icepick] remove most tslint rule exceptions and apply formatting.

* [icepick] remove unnecessary generics

* [icepick] update library version to 2.3 as that is when dissocIn/unsetIn where added.

* [icepick] apply strict-export-declare-modifiers rule

* [icepick] apply prefer-const rule to tests
This commit is contained in:
Nathan Brown
2019-12-04 15:54:03 -08:00
committed by Ron Buckton
parent c6dea7c0bf
commit 4e7cbd4e64
4 changed files with 176 additions and 195 deletions
+129 -74
View File
@@ -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<typeof coll, number>(coll, "b", 3); // {a: 1, b: 3}
const coll = { a: 1, b: 2 };
const newColl = i.assoc<typeof coll>(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<typeof coll, number>(coll, "b", 3); // {a: 1, b: 3}
const coll = { a: 1, b: 2 };
const newColl = i.set<typeof coll>(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<typeof coll, string>(coll, ["c", "d"], "baz");
const newColl = i.assocIn<typeof coll>(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<typeof coll, string>(coll, ["c", "d"], "baz");
const newColl = i.setIn<typeof coll>(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<number>(["a", 2], 4)
.setIn<number>(["a", 1], 5)
.updateIn<number>(["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<number>("e", 2)
.set<number>("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<number> = [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);
}
+43 -40
View File
@@ -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 <https://github.com/ngbrown>, Tobias Cohen <https://github.com/tobico>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
export function freeze<T>(collection: T): T;
export function thaw<T>(collection: T): T;
export function assoc<T>(collection: T, key: number | string, value: any): T;
export function dissoc<T>(collection: T, key: number | string): T;
export function dissocIn<T>(collection: T, path: ReadonlyArray<number | string>): T;
export function assocIn<T>(collection: T, path: ReadonlyArray<number | string>, value: any): T;
export function getIn(collection: any, path: ReadonlyArray<number | string>): any;
export function updateIn<T>(
collection: T,
path: ReadonlyArray<number | string>,
callback: (value: any) => any,
): T;
export declare function freeze<T>(collection: T): T;
export declare function thaw<T>(collection: T): T;
export declare function assoc<T, V>(collection: T, key: number | string, value: V): T;
export declare function dissoc<T>(collection: T, key: number | string): T;
export declare function dissocIn<T>(collection: T, path: Array<number | string>): T;
export declare function assocIn<T, V>(collection: T, path: Array<number | string>, value: V): T;
export declare function getIn<T>(collection: T, path: Array<number | string>): any;
export declare function updateIn<T, V>(collection: T, path: Array<number | string>, 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<T>(target: T): T;
export function assign<T, S1>(target: T, source1: S1): T & S1;
export function assign<T, S1, S2>(target: T, s1: S1, s2: S2): T & S1 & S2;
export function assign<T, S1, S2, S3>(target: T, s1: S1, s2: S2, s3: S3): T & S1 & S2 & S3;
export function assign<T, S1, S2, S3, S4>(target: T, s1: S1, s2: S2, s3: S3, s4: S4): T & S1 & S2 & S3 & S4;
export declare function assign<T>(target: T): T;
export declare function assign<T, S1>(target: T, source1: S1): (T & S1);
export declare function assign<T, S1, S2>(target: T, s1: S1, s2: S2): (T & S1 & S2);
export declare function assign<T, S1, S2, S3>(target: T, s1: S1, s2: S2, s3: S3): (T & S1 & S2 & S3);
export declare function assign<T, S1, S2, S3, S4>(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<T, S1>(target: T, source: S1): T & S1;
export declare function merge<T, S1>(target: T, source: S1): (T & S1);
export function push<T>(array: ReadonlyArray<T>, element: T): T[];
export function pop<T>(array: ReadonlyArray<T>): T[];
export function shift<T>(array: ReadonlyArray<T>): T[];
export function unshift<T>(array: ReadonlyArray<T>, element: T): T[];
export function reverse<T>(array: ReadonlyArray<T>): T[];
export function sort<T>(array: ReadonlyArray<T>, compareFunction?: (a: T, b: T) => number): T[];
export function splice<T>(array: ReadonlyArray<T>, start: number, deleteCount: number, ...items: T[]): T[];
export function slice<T>(array: ReadonlyArray<T>, begin?: number, end?: number): T[];
export declare function push<T>(array: T[], element: T): T[];
export declare function pop<T>(array: T[]): T[];
export declare function shift<T>(array: T[]): T[];
export declare function unshift<T>(array: T[], element: T): T[];
export declare function reverse<T>(array: T[]): T[];
export declare function sort<T>(array: T[], compareFunction?: (a: T, b: T) => number): T[];
export declare function splice<T>(array: T[], start: number, deleteCount: number, ...items: T[]): T[];
export declare function slice<T>(array: T[], begin?: number, end?: number): T[];
export declare function map<T, U>(fn: (value: T) => U, array: T[]): U[];
export declare function filter<T>(fn: (value: T) => boolean, array: T[]): T[];
export function map<T, U>(fn: (value: T) => U, array: ReadonlyArray<T>): U[];
export function filter<T>(fn: (value: T) => boolean, array: ReadonlyArray<T>): T[];
interface IcepickWrapper<T> {
value(): T;
@@ -46,20 +49,20 @@ interface IcepickWrapper<T> {
freeze(): IcepickWrapper<T>;
thaw(): IcepickWrapper<T>;
assoc<V>(key: number | string, value: V): IcepickWrapper<T>;
set<V>(key: number | string, value: V): IcepickWrapper<T>;
assoc(key: number | string, value: any): IcepickWrapper<T>;
set(key: number | string, value: any): IcepickWrapper<T>;
dissoc(key: number | string): IcepickWrapper<T>;
unset(key: number | string): IcepickWrapper<T>;
assocIn<V>(path: Array<number | string>, value: V): IcepickWrapper<T>;
setIn<V>(path: Array<number | string>, value: V): IcepickWrapper<T>;
dissocIn<V>(path: Array<number | string>): IcepickWrapper<T>;
unsetIn<V>(path: Array<number | string>): IcepickWrapper<T>;
assocIn(path: Array<number | string>, value: any): IcepickWrapper<T>;
setIn(path: Array<number | string>, value: any): IcepickWrapper<T>;
dissocIn(path: Array<number | string>): IcepickWrapper<T>;
unsetIn(path: Array<number | string>): IcepickWrapper<T>;
getIn(path: Array<number | string>): IcepickWrapper<any>;
updateIn<V>(path: Array<number | string>, callback: (value: V) => V): IcepickWrapper<T>;
updateIn(path: Array<number | string>, callback: (value: any) => any): IcepickWrapper<T>;
assign<S1>(source1: S1): IcepickWrapper<T & S1>;
assign<S1, S2>(s1: S1, s2: S2): IcepickWrapper<T & S1 & S2>;
@@ -73,4 +76,4 @@ interface IcepickWrapper<T> {
merge<S1>(source: S1): IcepickWrapper<T & S1>;
}
export declare function chain<T>(target: T): IcepickWrapper<T>;
export function chain<T>(target: T): IcepickWrapper<T>;
+3 -4
View File
@@ -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": [
"../"
+1 -77
View File
@@ -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": {}
}