diff --git a/package.json b/package.json index 0abd07a..b5b5d30 100644 --- a/package.json +++ b/package.json @@ -68,8 +68,7 @@ "dependencies": { "@types/jest-image-snapshot": "^6.1.0", "chalk": "^4.1.2", - "jest-image-snapshot": "^6.1.0", - "just-extend": "^6.2.0" + "jest-image-snapshot": "^6.1.0" }, "volta": { "node": "18.16.0", diff --git a/src/command.ts b/src/command.ts index c80865f..3500f8c 100644 --- a/src/command.ts +++ b/src/command.ts @@ -1,4 +1,4 @@ -import extend from 'just-extend' +import extend from './util/extend' import {MATCH, RECORD, RM} from './constants' import type { CypressImageSnapshotOptions, diff --git a/src/util/extend.d.ts b/src/util/extend.d.ts new file mode 100644 index 0000000..54287ba --- /dev/null +++ b/src/util/extend.d.ts @@ -0,0 +1,3 @@ +declare function extend(obj1: object, ...objn: any[]): object +declare function extend(deep: boolean, obj1: object, ...objn: any[]): object +export default extend diff --git a/src/util/extend.js b/src/util/extend.js new file mode 100644 index 0000000..04ca777 --- /dev/null +++ b/src/util/extend.js @@ -0,0 +1,77 @@ +/* eslint-disable no-var, prefer-rest-params */ +// https://github.com/angus-c/just/blob/master/packages/object-extend/index.mjs +var objectExtend = extend + +/* + var obj = {a: 3, b: 5}; + extend(obj, {a: 4, c: 8}); // {a: 4, b: 5, c: 8} + obj; // {a: 4, b: 5, c: 8} + + var obj = {a: 3, b: 5}; + extend({}, obj, {a: 4, c: 8}); // {a: 4, b: 5, c: 8} + obj; // {a: 3, b: 5} + + var arr = [1, 2, 3]; + var obj = {a: 3, b: 5}; + extend(obj, {c: arr}); // {a: 3, b: 5, c: [1, 2, 3]} + arr.push(4); + obj; // {a: 3, b: 5, c: [1, 2, 3, 4]} + + var arr = [1, 2, 3]; + var obj = {a: 3, b: 5}; + extend(true, obj, {c: arr}); // {a: 3, b: 5, c: [1, 2, 3]} + arr.push(4); + obj; // {a: 3, b: 5, c: [1, 2, 3]} + + extend({a: 4, b: 5}); // {a: 4, b: 5} + extend({a: 4, b: 5}, 3); {a: 4, b: 5} + extend({a: 4, b: 5}, true); {a: 4, b: 5} + extend('hello', {a: 4, b: 5}); // throws + extend(3, {a: 4, b: 5}); // throws +*/ + +function extend(/* [deep], obj1, obj2, [objn] */) { + var args = [].slice.call(arguments) + var deep = false + if (typeof args[0] == 'boolean') { + deep = args.shift() + } + var result = args[0] + if (isUnextendable(result)) { + throw new Error('extendee must be an object') + } + var extenders = args.slice(1) + var len = extenders.length + for (var i = 0; i < len; i++) { + var extender = extenders[i] + for (var key in extender) { + if (Object.prototype.hasOwnProperty.call(extender, key)) { + var value = extender[key] + if (deep && isCloneable(value)) { + var base = Array.isArray(value) ? [] : {} + result[key] = extend( + true, + Object.prototype.hasOwnProperty.call(result, key) && + !isUnextendable(result[key]) + ? result[key] + : base, + value, + ) + } else { + result[key] = value + } + } + } + } + return result +} + +function isCloneable(obj) { + return Array.isArray(obj) || {}.toString.call(obj) == '[object Object]' +} + +function isUnextendable(val) { + return !val || (typeof val != 'object' && typeof val != 'function') +} + +export {objectExtend as default} diff --git a/tsconfig.json b/tsconfig.json index b1c8697..e2c2190 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "module": "commonjs", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, + "allowJs": true, "strict": true, "skipLibCheck": true, "declaration": true, diff --git a/yarn.lock b/yarn.lock index 0400476..86b5edf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1014,7 +1014,6 @@ __metadata: eslint-plugin-prettier: ^4.2.1 http-server: ^14.1.1 jest-image-snapshot: ^6.1.0 - just-extend: ^6.2.0 npm-run-all: ^4.1.5 prettier: ^2.8.8 replace-json-property: ^1.9.0 @@ -4975,13 +4974,6 @@ __metadata: languageName: node linkType: hard -"just-extend@npm:^6.2.0": - version: 6.2.0 - resolution: "just-extend@npm:6.2.0" - checksum: 022024d6f687c807963b97a24728a378799f7e4af7357d1c1f90dedb402943d5c12be99a5136654bed8362c37a358b1793feaad3366896f239a44e17c5032d86 - languageName: node - linkType: hard - "kind-of@npm:^6.0.3": version: 6.0.3 resolution: "kind-of@npm:6.0.3"