Skip to content

Commit

Permalink
Merge pull request #119 from msssk/add-typescript-defs
Browse files Browse the repository at this point in the history
Add TypeScript definitions and test
  • Loading branch information
TehShrike committed Oct 3, 2018
2 parents 38f03f0 + 0d8bd06 commit af971b1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
14 changes: 14 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare function deepmerge<T>(x: Partial<T>, y: Partial<T>, options?: deepmerge.Options): T;
declare function deepmerge<T1, T2>(x: Partial<T1>, y: Partial<T2>, options?: deepmerge.Options): T1 & T2;

declare namespace deepmerge {
export interface Options {
arrayMerge?(target: any[], source: any[], options?: Options): any[];
clone?: boolean;
isMergeableObject?(value: object): boolean;
}

export function all (objects: object[], options?: Options): object;
}

export default deepmerge;
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"scripts": {
"build": "rollup -c",
"test": "npm run build && tap test/*.js && jsmd readme.md",
"test": "npm run build && tap test/*.js && jsmd readme.md && tsc --noEmit test/typescript.ts",
"size": "npm run build && uglifyjs --compress --mangle -- ./dist/umd.js | gzip -c | wc -c"
},
"devDependencies": {
Expand All @@ -34,6 +34,7 @@
"rollup-plugin-commonjs": "8.2.1",
"rollup-plugin-node-resolve": "3.0.0",
"tap": "~7.1.2",
"typescript": "=2.2.2",
"uglify-js": "^3.3.12"
},
"license": "MIT"
Expand Down
50 changes: 50 additions & 0 deletions test/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import merge from '../';

const x = {
foo: 'abc',
bar: 'def',
}

const y = {
foo: 'cba',
bar: 'fed',
}

const z = {
baz: '123',
quux: '456',
}

let merged1 = merge(x, y);
let merged2 = merge(x, z);
let merged3 = merge.all([ x, y, z ]);

merged1.foo;
merged1.bar;
merged2.foo;
merged2.baz;

const options1: merge.Options = {
clone: true,
isMergeableObject (obj) {
return true;
},
};

const options2: merge.Options = {
arrayMerge (target, source, options) {
target.length;
source.length;
options.isMergeableObject(target);

return [];
},
clone: true,
isMergeableObject (obj) {
return true;
},
};

merged1 = merge(x, y, options1);
merged2 = merge(x, z, options2);
merged3 = merge([x, y, z], options1);

0 comments on commit af971b1

Please sign in to comment.