-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: inline just-extend to prevent build failures
Fixes: #12
- Loading branch information
1 parent
7eb876d
commit c498800
Showing
6 changed files
with
83 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters