Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript definitions and test #119

Merged
merged 4 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);