Icepick: initial definitions.

This commit is contained in:
Nathan Brown
2016-01-04 16:54:58 -07:00
parent bdaf2f0eb1
commit 8ab63af57c
2 changed files with 249 additions and 0 deletions

177
icepick/icepick-tests.ts Normal file
View File

@@ -0,0 +1,177 @@
/// <reference path="icepick.d.ts" />
/// <reference path="../underscore/underscore.d.ts" />
import i = require("icepick");
"use strict"; // so attempted modifications of frozen objects will throw errors
// freeze(collection)
{
let coll = {
a: "foo",
b: [1, 2, 3],
c: {
d: "bar"
}
};
i.freeze(coll);
}
// thaw(collection)
class Foo {}
{
let coll = i.freeze({ a: "foo", b: [1, 2, 3], c: { d: "bar" }, e: new Foo() });
let 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}
let arr = ["a", "b", "c"];
let 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}
let arr = ["a", "b", "c"];
let 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}
let arr = ["a", "b", "c"];
let 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}
let arr = ["a", "b", "c"];
let newArr = i.unset(arr, 2); // ["a", , "c"]
}
// assocIn(collection, path, value)
{
let coll = {
a: "foo",
b: [1, 2, 3],
c: {
d: "bar"
}
};
let newColl = i.assocIn(coll, ["c", "d"], "baz");
let coll2 = {};
let newColl2 = i.assocIn(coll2, ["a", "b", "c"], 1);
}
// alias: setIn(collection, path, value)
{
let coll = {
a: "foo",
b: [1, 2, 3],
c: {
d: "bar"
}
};
let newColl = i.setIn(coll, ["c", "d"], "baz");
let coll2 = {};
let newColl2 = i.setIn(coll2, ["a", "b", "c"], 1);
}
// getIn(collection, path)
{
let coll = i.freeze([
{ a: 1 },
{ b: 2 }
]);
let result = i.getIn(coll, [1, "b"]); // 2
}
// updateIn(collection, path, callback)
{
let coll = i.freeze([
{ a: 1 },
{ b: 2 }
]);
let newColl = i.updateIn(coll, [1, "b"], function(val: number) {
return val * 2;
}); // [ {a: 1}, {b: 4} ]
}
// assign(coll1, coll2, ...)
{
let obj1 = { a: 1, b: 2, c: 3 };
let obj2 = { c: 4, d: 5 };
let result = i.assign(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 } };
let 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);
(result1 === result2); // true
}
// arrays
{
var 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];
}
{
i.map(function(v) { return v * 2 }, [1, 2, 3]); // [2, 4, 6]
var removeEvens = _.partial(i.filter, function(v: number) { return v % 2; });
removeEvens([1, 2, 3]); // [1, 3]
}
{
var arr = i.freeze([{ a: 1 }, { b: 2 }]);
//ECMAScript 2015
//arr.find(function(item) { return item.b != null; }); // {b: 2}
}
// chain(coll) - not defined
{
let o = {
a: [1, 2, 3],
b: { c: 1 },
d: 4
};
let result = i.chain(o)
.assocIn(["a", 2], 4)
.merge({ b: { c: 2, c2: 3 } })
.assoc("e", 2)
.dissoc("d")
.value();
}

72
icepick/icepick.d.ts vendored Normal file
View File

@@ -0,0 +1,72 @@
// Type definitions for icepick v1.1.0
// Project: https://github.com/aearly/icepick
// Definitions by: Nathan Brown <https://github.com/ngbrown>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module "icepick" {
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 assocIn<T>(collection: T, path: Array<number | string>, value: any): T;
export function getIn<Result>(collection: any, path: Array<number | string>): Result;
export function updateIn<T, V>(collection: T, path: Array<number | string>, callback: (value: V) => V): T;
export {assoc as set};
export {dissoc as unset};
export {assocIn as setIn};
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 {assign as extend};
export function merge<T, S1>(target: T, source: S1): (T & S1);
export function push<T>(array: T[], element: T): T[];
export function pop<T>(array: T[]): T[];
export function shift<T>(array: T[]): T[];
export function unshift<T>(array: T[], element: T): T[];
export function reverse<T>(array: T[]): T[];
export function sort<T>(array: T[], compareFunction?: (a:T, b:T) => number): T[];
export function splice<T>(array: T[], start: number, deleteCount: number, ...items: T[]): T[];
export function slice<T>(array: T[], begin?: number, end?: number): T[];
export function map<T, U>(fn: (value: T) => U, array: T[]): U[];
export function filter<T>(fn: (value: T) => boolean, array: T[]): T[];
interface IcepickWrapper<T> {
value(): T;
freeze(): IcepickWrapper<T>;
thaw(): 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(path: Array<number | string>, value: any): IcepickWrapper<T>;
setIn(path: Array<number | string>, value: any): IcepickWrapper<T>;
getIn<Result>(collection: any, path: Array<number | string>): IcepickWrapper<Result>;
updateIn<T, V>(collection: T, path: Array<number | string>, callback: (value: V) => V): IcepickWrapper<T>;
assign<S1>(source1: S1): IcepickWrapper<T & S1>;
assign<S1, S2>(s1: S1, s2: S2): IcepickWrapper<T & S1 & S2>;
assign<S1, S2, S3>(s1: S1, s2: S2, s3: S3): IcepickWrapper<T & S1 & S2 & S3>;
assign<S1, S2, S3, S4>(s1: S1, s2: S2, s3: S3, s4: S4): IcepickWrapper<T & S1 & S2 & S3 & S4>;
extend<S1>(source1: S1): IcepickWrapper<T & S1>;
extend<S1, S2>(s1: S1, s2: S2): IcepickWrapper<T & S1 & S2>;
extend<S1, S2, S3>(s1: S1, s2: S2, s3: S3): IcepickWrapper<T & S1 & S2 & S3>;
extend<S1, S2, S3, S4>(s1: S1, s2: S2, s3: S3, s4: S4): IcepickWrapper<T & S1 & S2 & S3 & S4>;
merge<S1>(source: S1): IcepickWrapper<T & S1>;
}
export function chain<T>(target: T): IcepickWrapper<T>;
}