defaults
Advanced tools
Comparing version 1.0.4 to 2.0.0
58
index.js
@@ -1,13 +0,51 @@ | ||
var clone = require('clone'); | ||
// From: https://github.com/sindresorhus/is-plain-obj | ||
const isPlainObject = value => { | ||
if (typeof value !== 'object' || value === null) { | ||
return false; | ||
} | ||
module.exports = function(options, defaults) { | ||
options = options || {}; | ||
const prototype = Object.getPrototypeOf(value); | ||
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value); | ||
}; | ||
Object.keys(defaults).forEach(function(key) { | ||
if (typeof options[key] === 'undefined') { | ||
options[key] = clone(defaults[key]); | ||
} | ||
}); | ||
const disallowedKeys = new Set([ | ||
'__proto__', | ||
'prototype', | ||
'constructor', | ||
]); | ||
return options; | ||
}; | ||
const merge = (destination, source) => { | ||
if (!isPlainObject(source)) { | ||
return destination; | ||
} | ||
if (!destination) { | ||
destination = {}; | ||
} | ||
for (const [sourceKey, sourceValue] of Object.entries(source)) { | ||
if (disallowedKeys.has(sourceKey)) { | ||
continue; | ||
} | ||
const destinationValue = destination[sourceKey]; | ||
if (isPlainObject(destinationValue) && isPlainObject(sourceValue)) { | ||
destination[sourceKey] = merge(destinationValue, sourceValue); // Merge plain objects recursively | ||
} else if (sourceValue === undefined) { | ||
continue; // Skip undefined values in source | ||
} else if (isPlainObject(sourceValue)) { | ||
destination[sourceKey] = merge({}, sourceValue); // Clone plain objects | ||
} else if (Array.isArray(sourceValue)) { | ||
destination[sourceKey] = [...sourceValue]; // Clone arrays | ||
} else { | ||
destination[sourceKey] = sourceValue; // Assign other types | ||
} | ||
} | ||
return destination; | ||
}; | ||
export default function defaults(options = {}, defaultOptions = {}) { | ||
return merge({...defaultOptions}, {...options}); | ||
} |
{ | ||
"name": "defaults", | ||
"version": "1.0.4", | ||
"description": "merge single level defaults over a config object", | ||
"main": "index.js", | ||
"version": "2.0.0", | ||
"description": "Easily handle defaults for your options", | ||
"license": "MIT", | ||
"repository": "sindresorhus/defaults", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"type": "module", | ||
"exports": { | ||
"types": "./index.d.ts", | ||
"default": "./index.js" | ||
}, | ||
"engines": { | ||
"node": ">=16" | ||
}, | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "xo && ava && tsd" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/sindresorhus/node-defaults.git" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
@@ -24,11 +33,7 @@ "config", | ||
], | ||
"author": "Elijah Insua <tmpvar@gmail.com>", | ||
"license": "MIT", | ||
"readmeFilename": "README.md", | ||
"dependencies": { | ||
"clone": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"tap": "^2.0.0" | ||
"ava": "^5.2.0", | ||
"tsd": "^0.28.1", | ||
"xo": "^0.54.0" | ||
} | ||
} |
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
5477
0
75
62
0
Yes
3
1
- Removedclone@^1.0.2
- Removedclone@1.0.4(transitive)