metalsmith-default-values
Advanced tools
Comparing version 1.0.1 to 2.0.0
'use strict'; | ||
var debug_lib = require('debug'); | ||
var debug = debug_lib('metalsmith-set-defaults'); | ||
var error = debug_lib('metalsmith-set-defaults:error'); | ||
var multimatch = require('multimatch'); | ||
var Joi = require('joi'); | ||
var set_defaults = require('./set_defaults'); | ||
const debug_lib = require('debug'); | ||
const package_name = 'metalsmith-set-defaults'; | ||
const debug = debug_lib(package_name); | ||
const error = debug_lib(`${package_name}:error`); | ||
const multimatch = require('multimatch'); | ||
const Joi = require('joi'); | ||
const set_defaults = require('./set_defaults'); | ||
var schema = Joi.array().min(1).items( | ||
const schema = Joi.array().min(1).items( | ||
Joi.object().keys({ | ||
@@ -18,51 +19,46 @@ pattern : [Joi.array().min(1), Joi.string()], | ||
/** | ||
* Sets default values based upon config | ||
* Sets default values based upon config passed | ||
* | ||
* @return {function} | ||
* @param {array} config See schema for constraints | ||
* | ||
* @return {function} Metalmsith Plugin | ||
*/ | ||
const default_values = (config) => (files, metalsmith, done) => { | ||
// Check config fits schema | ||
const validation = schema.validate(config, {allowUnknown: true}); | ||
if (validation.error) { | ||
error('Validation failed, %o', validation.error.details[0].message); | ||
return done(validation.error); | ||
} | ||
// Convert string patterns to be in an array for multimatch | ||
config = validation.value.map((item) => { | ||
item.pattern = item.pattern || '*'; | ||
item.pattern = (typeof item.pattern === 'string') ? [item.pattern] : item.pattern; | ||
return item; | ||
}); | ||
var default_values = function (config) { | ||
return function (files, metalsmith, done) { | ||
// Check config fits schema | ||
var schema_err; | ||
schema.validate(config, function (err, value) { | ||
if (err) { | ||
error('Validation failed, %o', err.details[0].message); | ||
schema_err = err; | ||
debug('config: ', config); | ||
// Loop through files | ||
Object.keys(files).forEach(function (file) { | ||
// Loop through configurations | ||
config.forEach(function (item) { | ||
if (!item.set_defaults) { | ||
// initialise set_defaults function | ||
item.set_defaults = set_defaults(item.defaults); | ||
} | ||
// Convert string patterns to be in an array for multimatch | ||
config = value.map(function (item) { | ||
item.pattern = item.pattern || '*'; | ||
item.pattern = (typeof item.pattern === 'string') ? [item.pattern] : item.pattern; | ||
return item; | ||
}); | ||
}); | ||
if (schema_err) { | ||
return done(schema_err); | ||
} | ||
debug('config: ', config); | ||
// Loop through files | ||
Object.keys(files).forEach(function (file) { | ||
// Loop through configurations | ||
config.forEach(function (item) { | ||
if (!item.set_defaults) { | ||
// initialise set_defaults function | ||
item.set_defaults = set_defaults(item.defaults); | ||
} | ||
debug('Checking: %s; Against: %s', file, item.pattern); | ||
if (multimatch(file, item.pattern).length) { | ||
debug('Passed - Will set defaults if required'); | ||
debug('Checking: %s; Against: %s', file, item.pattern); | ||
if (multimatch(file, item.pattern).length) { | ||
debug('Passed - Will set defaults if required'); | ||
// Set defaults if required | ||
item.set_defaults(files[file]); | ||
} | ||
}); | ||
// Set defaults if required | ||
item.set_defaults(files[file]); | ||
} | ||
}); | ||
}); | ||
return done(); | ||
}; | ||
return done(); | ||
}; | ||
module.exports = default_values; |
'use strict'; | ||
var debug = require('debug')('metalsmith-set-defaults:set-defaults'); | ||
var get = require('lodash.get'); | ||
var set = require('lodash.set'); | ||
const debug = require('debug')('metalsmith-set-defaults:set-defaults'); | ||
const get = require('lodash.get'); | ||
const set = require('lodash.set'); | ||
@@ -15,21 +15,19 @@ /** | ||
*/ | ||
var set_defaults = function (defaults) { | ||
return function (item) { | ||
debug('defaults: %o', defaults); | ||
Object.keys(defaults).forEach(function (key) { | ||
var value = get(item, key); | ||
const set_defaults = (defaults) => (item) => { | ||
debug('defaults: %o', defaults); | ||
Object.keys(defaults).forEach((key) => { | ||
const value = get(item, key); | ||
// For more verbose debugging enable the following | ||
// debug('key: %s', key); | ||
// debug('default: %s', defaults[key]); | ||
// debug('value: %s', value); | ||
// For more verbose debugging enable the following | ||
// debug('key: %s', key); | ||
// debug('default: %s', defaults[key]); | ||
// debug('value: %s', value); | ||
if (value === void 0 || value === null) { | ||
set(item, key, defaults[key]); | ||
} | ||
}); | ||
return item; | ||
}; | ||
if (value === void 0 || value === null) { | ||
set(item, key, defaults[key]); | ||
} | ||
}); | ||
return item; | ||
}; | ||
module.exports = set_defaults; |
{ | ||
"ignore": [], | ||
"ignore": ["node_modules"], | ||
"ext": "js", | ||
"delay": "100ms" | ||
} |
{ | ||
"name": "metalsmith-default-values", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "A metalsmith plugin for setting default values for frontmatter metadata.", | ||
"engines": { | ||
"node": ">=4.x" | ||
}, | ||
"dependencies": { | ||
"debug": "^2.2.0", | ||
"joi": "^9.0.4", | ||
"debug": "^2.6.8", | ||
"joi": "^10.6.0", | ||
"lodash.get": "^4.4.2", | ||
@@ -13,5 +16,5 @@ "lodash.set": "^4.3.2", | ||
"devDependencies": { | ||
"eslint": "^2.10.2", | ||
"nodemon": "^1.10.2", | ||
"tape": "^4.6.0", | ||
"eslint": "^4.4.0", | ||
"nodemon": "^1.11.0", | ||
"tap": "^10.7.1", | ||
"taper": "^0.5.0" | ||
@@ -18,0 +21,0 @@ }, |
@@ -19,3 +19,3 @@ # metalsmith-default-values | ||
```js | ||
var default_values = require('metalsmith-default_values'); | ||
var default_values = require('metalsmith-default-values'); | ||
``` | ||
@@ -22,0 +22,0 @@ |
'use strict'; | ||
var test = require('tape'); | ||
const test = require('tap').test; | ||
@@ -8,6 +8,6 @@ // TODO: Add more tests to cover edge cases | ||
// metalsmith_default_values plugin | ||
test('metalsmith-default-values sets a key when not present', function (assert) { | ||
var default_values_lib = require('../lib'); | ||
test('metalsmith-default-values sets a key when not present', (assert) => { | ||
const default_values_lib = require('../lib'); | ||
var default_values = default_values_lib([ | ||
const default_values = default_values_lib([ | ||
{ | ||
@@ -21,3 +21,3 @@ defaults: {default_val: true} | ||
]); | ||
var actual = { | ||
const actual = { | ||
'file1': { | ||
@@ -31,3 +31,3 @@ existing_key: 'yes' | ||
}; | ||
var expected = { | ||
const expected = { | ||
'file1': { | ||
@@ -43,3 +43,3 @@ existing_key: 'yes', | ||
}; | ||
default_values(actual, void 0, function (err) { | ||
default_values(actual, void 0, (err) => { | ||
assert.ifError(err, 'Has not errored'); | ||
@@ -46,0 +46,0 @@ assert.isEquivalent(actual, expected, 'Defaults set where key not present'); |
'use strict'; | ||
var test = require('tape'); | ||
var set_defaults_lib = require('../lib/set_defaults'); | ||
const test = require('tap').test; | ||
const set_defaults_lib = require('../lib/set_defaults'); | ||
test('set-defaults initialisation returns a function', function (assert) { | ||
var actual = set_defaults_lib(); | ||
test('set-defaults initialisation returns a function', (assert) => { | ||
const actual = set_defaults_lib(); | ||
assert.isEqual(typeof actual, 'function', 'Function returned after initialisation'); | ||
assert.end(); | ||
assert.isEqual(typeof actual, 'function', 'Function returned after initialisation'); | ||
}); | ||
test('set-defaults sets a key when not present', function (assert) { | ||
var defaults = { | ||
test('set-defaults sets a key when not present', (assert) => { | ||
const defaults = { | ||
default_val: true | ||
}; | ||
var set_defaults = set_defaults_lib(defaults); | ||
const set_defaults = set_defaults_lib(defaults); | ||
assert.isEqual(typeof set_defaults, 'function', 'Function returned after initialisation'); | ||
var actual = set_defaults({ | ||
const actual = set_defaults({ | ||
initial: 'yes' | ||
}); | ||
var expected = { | ||
const expected = { | ||
initial : 'yes', | ||
@@ -24,0 +24,0 @@ default_val: true |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
47215
151
+ Addedjoi@10.6.0(transitive)
- Removedjoi@9.2.0(transitive)
- Removedmoment@2.30.1(transitive)
Updateddebug@^2.6.8
Updatedjoi@^10.6.0