node-powertools
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -401,7 +401,7 @@ (function (root, factory) { | ||
Powertools.defaults = function (user, defaults) { | ||
Powertools.defaults = function (user, schema) { | ||
var updatedSettings = {}; | ||
var alreadyDone = []; | ||
getKeys(defaults) | ||
getKeys(schema) | ||
.forEach(function (key) { | ||
@@ -419,22 +419,30 @@ // Get the path to this setting, minus the last key | ||
var userSetting = getNestedValue(user, pathMinusLast); | ||
var planDefault = getNestedValue(defaults, pathMinusLast); | ||
var schemaDefault = getNestedValue(schema, pathMinusLast); | ||
var workingValue; | ||
// If the plan default is not an object, make it one | ||
if (!planDefault || typeof planDefault === 'undefined') { | ||
planDefault = { | ||
default: getNestedValue(defaults, key), | ||
if (!schemaDefault || typeof schemaDefault === 'undefined') { | ||
schemaDefault = { | ||
default: getNestedValue(schema, key), | ||
} | ||
} | ||
// Set the defaults of the plan default | ||
planDefault.default = typeof planDefault.default === 'undefined' ? undefined : planDefault.default; | ||
planDefault.value = typeof planDefault.value === 'undefined' ? undefined : planDefault.value; | ||
planDefault.types = planDefault.types || ['any']; | ||
planDefault.min = planDefault.min || 0; | ||
planDefault.max = planDefault.max || Infinity; | ||
// Set the schema of the plan default | ||
schemaDefault.types = schemaDefault.types || ['any']; | ||
schemaDefault.value = typeof schemaDefault.value === 'undefined' ? undefined : schemaDefault.value; | ||
schemaDefault.default = typeof schemaDefault.default === 'undefined' ? undefined : schemaDefault.default; | ||
schemaDefault.min = schemaDefault.min || 0; | ||
schemaDefault.max = schemaDefault.max || Infinity; | ||
// Run functions | ||
if (typeof schemaDefault.value === 'function') { | ||
schemaDefault.value = schemaDefault.value(); | ||
} | ||
if (typeof schemaDefault.default === 'function') { | ||
schemaDefault.default = schemaDefault.default(); | ||
} | ||
// If the user has not set a value for this setting, use the plan default | ||
if (typeof userSetting === 'undefined') { | ||
workingValue = planDefault.default; | ||
workingValue = schemaDefault.default; | ||
} else { | ||
@@ -445,10 +453,10 @@ workingValue = userSetting; | ||
// Loop through acceptable types of default and set default if it is not one of them | ||
workingValue = enforceValidTypes(workingValue, planDefault.types, planDefault.default); | ||
workingValue = enforceValidTypes(workingValue, schemaDefault.types, schemaDefault.default); | ||
// Enforce min and max values | ||
workingValue = enforceMinMax(workingValue, planDefault.min, planDefault.max); | ||
workingValue = enforceMinMax(workingValue, schemaDefault.min, schemaDefault.max); | ||
// Force to value if it is set | ||
if (typeof planDefault.value !== 'undefined') { | ||
workingValue = planDefault.value; | ||
if (typeof schemaDefault.value !== 'undefined') { | ||
workingValue = schemaDefault.value; | ||
} | ||
@@ -455,0 +463,0 @@ |
{ | ||
"name": "node-powertools", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Powerful assistive functions for Node and Browser environments.", | ||
@@ -33,2 +33,2 @@ "main": "dist/index.js", | ||
} | ||
} | ||
} |
@@ -401,7 +401,7 @@ (function (root, factory) { | ||
Powertools.defaults = function (user, defaults) { | ||
Powertools.defaults = function (user, schema) { | ||
var updatedSettings = {}; | ||
var alreadyDone = []; | ||
getKeys(defaults) | ||
getKeys(schema) | ||
.forEach(function (key) { | ||
@@ -419,22 +419,30 @@ // Get the path to this setting, minus the last key | ||
var userSetting = getNestedValue(user, pathMinusLast); | ||
var planDefault = getNestedValue(defaults, pathMinusLast); | ||
var schemaDefault = getNestedValue(schema, pathMinusLast); | ||
var workingValue; | ||
// If the plan default is not an object, make it one | ||
if (!planDefault || typeof planDefault === 'undefined') { | ||
planDefault = { | ||
default: getNestedValue(defaults, key), | ||
if (!schemaDefault || typeof schemaDefault === 'undefined') { | ||
schemaDefault = { | ||
default: getNestedValue(schema, key), | ||
} | ||
} | ||
// Set the defaults of the plan default | ||
planDefault.default = typeof planDefault.default === 'undefined' ? undefined : planDefault.default; | ||
planDefault.value = typeof planDefault.value === 'undefined' ? undefined : planDefault.value; | ||
planDefault.types = planDefault.types || ['any']; | ||
planDefault.min = planDefault.min || 0; | ||
planDefault.max = planDefault.max || Infinity; | ||
// Set the schema of the plan default | ||
schemaDefault.types = schemaDefault.types || ['any']; | ||
schemaDefault.value = typeof schemaDefault.value === 'undefined' ? undefined : schemaDefault.value; | ||
schemaDefault.default = typeof schemaDefault.default === 'undefined' ? undefined : schemaDefault.default; | ||
schemaDefault.min = schemaDefault.min || 0; | ||
schemaDefault.max = schemaDefault.max || Infinity; | ||
// Run functions | ||
if (typeof schemaDefault.value === 'function') { | ||
schemaDefault.value = schemaDefault.value(); | ||
} | ||
if (typeof schemaDefault.default === 'function') { | ||
schemaDefault.default = schemaDefault.default(); | ||
} | ||
// If the user has not set a value for this setting, use the plan default | ||
if (typeof userSetting === 'undefined') { | ||
workingValue = planDefault.default; | ||
workingValue = schemaDefault.default; | ||
} else { | ||
@@ -445,10 +453,10 @@ workingValue = userSetting; | ||
// Loop through acceptable types of default and set default if it is not one of them | ||
workingValue = enforceValidTypes(workingValue, planDefault.types, planDefault.default); | ||
workingValue = enforceValidTypes(workingValue, schemaDefault.types, schemaDefault.default); | ||
// Enforce min and max values | ||
workingValue = enforceMinMax(workingValue, planDefault.min, planDefault.max); | ||
workingValue = enforceMinMax(workingValue, schemaDefault.min, schemaDefault.max); | ||
// Force to value if it is set | ||
if (typeof planDefault.value !== 'undefined') { | ||
workingValue = planDefault.value; | ||
if (typeof schemaDefault.value !== 'undefined') { | ||
workingValue = schemaDefault.value; | ||
} | ||
@@ -455,0 +463,0 @@ |
@@ -298,2 +298,6 @@ const package = require('../package.json'); | ||
}, | ||
time: { | ||
types: ['string'], | ||
default: () => '2999-01-01T00:00:00.000Z', | ||
}, | ||
stats: { | ||
@@ -323,2 +327,6 @@ level: { | ||
}, | ||
time: { | ||
types: ['string'], | ||
value: () => '2999-01-01T00:00:00.000Z', | ||
}, | ||
stats: { | ||
@@ -454,2 +462,14 @@ level: { | ||
it('should work with functions', () => { | ||
const user = { | ||
time: '2999-01-01T00:00:00.000Z', | ||
}; | ||
const planId = 'premium'; | ||
const result = powertools.defaults(user, defaults[planId]); | ||
assert.strictEqual(result.time, user.time); | ||
}); | ||
// it('should work without requiring strict defaults', () => { | ||
@@ -456,0 +476,0 @@ // const user = { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60059
1323