metalsmith-default-values
Advanced tools
Comparing version 2.0.0 to 2.0.1
'use strict'; | ||
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 debug_lib = require('debug'); | ||
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'); | ||
const schema = Joi.array().min(1).items( | ||
const schema = Joi.array() | ||
.min(1) | ||
.items( | ||
Joi.object().keys({ | ||
pattern : [Joi.array().min(1), Joi.string()], | ||
defaults: Joi.object().min(1) | ||
pattern: [Joi.array().min(1), Joi.string()], | ||
defaults: Joi.object().min(1) | ||
}) | ||
); | ||
); | ||
@@ -25,40 +28,41 @@ /** | ||
*/ | ||
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; | ||
}); | ||
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; | ||
}); | ||
debug('config: ', config); | ||
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); | ||
} | ||
// 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'; | ||
const debug = require('debug')('metalsmith-set-defaults:set-defaults'); | ||
const get = require('lodash.get'); | ||
const set = require('lodash.set'); | ||
const get = require('lodash.get'); | ||
const set = require('lodash.set'); | ||
@@ -15,19 +15,19 @@ /** | ||
*/ | ||
const set_defaults = (defaults) => (item) => { | ||
debug('defaults: %o', defaults); | ||
Object.keys(defaults).forEach((key) => { | ||
const 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; |
{ | ||
"name": "metalsmith-default-values", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "A metalsmith plugin for setting default values for frontmatter metadata.", | ||
@@ -9,4 +9,4 @@ "engines": { | ||
"dependencies": { | ||
"debug": "^2.6.8", | ||
"joi": "^10.6.0", | ||
"debug": "^3.1.0", | ||
"joi": "^12.0.0", | ||
"lodash.get": "^4.4.2", | ||
@@ -18,12 +18,15 @@ "lodash.set": "^4.3.2", | ||
"eslint": "^4.4.0", | ||
"eslint-config-prettier": "^2.9.0", | ||
"eslint-plugin-prettier": "^2.5.0", | ||
"nodemon": "^1.11.0", | ||
"tap": "^10.7.1", | ||
"taper": "^0.5.0" | ||
"prettier": "^1.10.2", | ||
"tap": "^11.0.1" | ||
}, | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "taper test", | ||
"test": "tap test", | ||
"posttest": "npm run lint", | ||
"lint": "eslint --ignore-pattern node_modules **/*.js", | ||
"dev": "nodemon --exec 'npm test'" | ||
"lint": "eslint --cache --fix .", | ||
"dev": "nodemon --exec 'npm test'", | ||
"postversion": "git push && git push --tags && npm publish" | ||
}, | ||
@@ -30,0 +33,0 @@ "repository": { |
# metalsmith-default-values | ||
[![npm version][npm-badge]][npm-url] | ||
[![Build Status][travis-badge]][travis-url] | ||
[![code style: prettier][prettier-badge]][prettier-url] | ||
[![bitHound Dependencies][bithound-badge]][bithound-url] | ||
[![bitHound Dev Dependencies][bithound-dev-badge]][bithound-url] | ||
[![bitHound Code][bithound-code-badge]][bithound-code-url] | ||
Metalsmith Plugin for setting default front-matter metadata | ||
[![Known Vulnerabilities][snyk-badge]][synk-url] | ||
[![NSP Status][nsp-badge]][nsp-url] | ||
[![Package Quality][quality-badge]][quality-url] | ||
Metalsmith Plugin for setting default front-matter metadata. | ||
Uses [multimatch](https://github.com/sindresorhus/multimatch#multimatch-) for pattern matching. | ||
## Installation | ||
```bash | ||
@@ -13,3 +24,3 @@ npm install -S metalsmith-default-values | ||
*Note*: `-S` switch saves the plugin to your `package.json`. | ||
_Note_: `-S` switch saves the plugin to your `package.json`. | ||
@@ -19,23 +30,40 @@ ## Usage: | ||
### 1. Include the plugin | ||
```js | ||
var default_values = require('metalsmith-default-values'); | ||
const default_values = require('metalsmith-default-values'); | ||
``` | ||
### 2. Use the plugin in your build pipeline | ||
```js | ||
... | ||
.use(default_values([ | ||
{ | ||
pattern : 'posts/*.md', | ||
defaults: { | ||
layout: 'post.hbs' | ||
} | ||
}, | ||
{ | ||
pattern : 'diary/*.md', | ||
defaults: { | ||
layout : 'diary.hbs', | ||
private: true | ||
} | ||
} | ||
{ | ||
pattern : 'posts/*.md', | ||
defaults: { | ||
layout: 'post.hbs' | ||
} | ||
}, | ||
{ | ||
pattern : 'diary/*.md', | ||
defaults: { | ||
layout : 'diary.hbs', | ||
private: true | ||
} | ||
}, | ||
{ | ||
pattern : [ | ||
'diary/*.md', | ||
'archive/**/*.md' | ||
], | ||
defaults: { | ||
no_index: true | ||
} | ||
}, | ||
{ | ||
pattern : '**/*.md', | ||
defaults: { | ||
layout : 'default.hbs' | ||
} | ||
} | ||
])) | ||
@@ -46,22 +74,39 @@ ... | ||
### 3. Profit | ||
[GL HF](http://www.urbandictionary.com/define.php?term=glhf) | ||
## Node versions | ||
Because [Joi](https://github.com/hapijs/joi/) > `v6` uses ES6 syntax this runs on NodeJS `v4` and above. | ||
## Contributions | ||
Make sure you have [EditorConfig plugin](http://editorconfig.org/#download) for your editor. | ||
`npm test` runs the tests, also uses [ESLint](https://eslint.org/) and [Prettier](https://prettier.io/) | ||
# License - GPL-3.0 | ||
metalsmith-default-values is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
metalsmith-default-values is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
metalsmith-default-values is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License along with metalsmith-default-values. If not, see http://www.gnu.org/licenses/. | ||
You should have received a copy of the GNU General Public License along with metalsmith-default-values. If not, see http://www.gnu.org/licenses/. | ||
[npm-badge]: https://img.shields.io/npm/v/metalsmith-default-values.svg | ||
[npm-url]: https://www.npmjs.com/package/metalsmith-default-values | ||
[travis-badge]: https://travis-ci.org/woodyrew/metalsmith-default-values.svg?branch=master | ||
[travis-url]: https://travis-ci.org/woodyrew/metalsmith-default-values | ||
[bithound-badge]: https://www.bithound.io/github/woodyrew/metalsmith-default-values/badges/dependencies.svg | ||
[bithound-dev-badge]: https://www.bithound.io/github/woodyrew/metalsmith-default-values/badges/devDependencies.svg | ||
[bithound-url]: https://www.bithound.io/github/woodyrew/metalsmith-default-values/master/dependencies/npm | ||
[bithound-code-badge]: https://www.bithound.io/github/woodyrew/metalsmith-default-values/badges/code.svg | ||
[bithound-code-url]: https://www.bithound.io/github/woodyrew/metalsmith-default-values | ||
[quality-badge]: http://npm.packagequality.com/shield/metalsmith-default-values.svg | ||
[quality-url]: http://packagequality.com/#?package=metalsmith-default-values | ||
[prettier-badge]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square | ||
[prettier-url]: https://github.com/prettier/prettier | ||
[snyk-badge]: https://snyk.io/test/github/woodyrew/metalsmith-default-values/badge.svg | ||
[synk-url]: https://snyk.io/test/github/woodyrew/metalsmith-default-values | ||
[nsp-badge]: https://nodesecurity.io/orgs/woodyrew/projects/14b90fd0-d8f1-4156-8e1c-d690a8c4f197/badge | ||
[nsp-url]: https://nodesecurity.io/orgs/woodyrew/projects/14b90fd0-d8f1-4156-8e1c-d690a8c4f197 |
@@ -5,42 +5,40 @@ 'use strict'; | ||
// TODO: Add more tests to cover edge cases | ||
// metalsmith_default_values plugin | ||
test('metalsmith-default-values sets a key when not present', (assert) => { | ||
const default_values_lib = require('../lib'); | ||
test('metalsmith-default-values sets a key when not present', assert => { | ||
const default_values_lib = require('../lib'); | ||
const default_values = default_values_lib([ | ||
{ | ||
defaults: {default_val: true} | ||
}, | ||
{ | ||
pattern : 'file2', | ||
defaults: {another_default: 'hello'} | ||
} | ||
]); | ||
const actual = { | ||
'file1': { | ||
existing_key: 'yes' | ||
}, | ||
'file2': { | ||
existing_key: 'yes', | ||
default_val : false | ||
} | ||
}; | ||
const expected = { | ||
'file1': { | ||
existing_key: 'yes', | ||
default_val : true | ||
}, | ||
'file2': { | ||
existing_key : 'yes', | ||
default_val : false, | ||
another_default: 'hello' | ||
} | ||
}; | ||
default_values(actual, void 0, (err) => { | ||
assert.ifError(err, 'Has not errored'); | ||
assert.isEquivalent(actual, expected, 'Defaults set where key not present'); | ||
assert.end(); | ||
}); | ||
const default_values = default_values_lib([ | ||
{ | ||
defaults: { default_val: true } | ||
}, | ||
{ | ||
pattern: 'file2', | ||
defaults: { another_default: 'hello' } | ||
} | ||
]); | ||
const actual = { | ||
file1: { | ||
existing_key: 'yes' | ||
}, | ||
file2: { | ||
existing_key: 'yes', | ||
default_val: false | ||
} | ||
}; | ||
const expected = { | ||
file1: { | ||
existing_key: 'yes', | ||
default_val: true | ||
}, | ||
file2: { | ||
existing_key: 'yes', | ||
default_val: false, | ||
another_default: 'hello' | ||
} | ||
}; | ||
default_values(actual, void 0, err => { | ||
assert.ifError(err, 'Has not errored'); | ||
assert.isEquivalent(actual, expected, 'Defaults set where key not present'); | ||
assert.end(); | ||
}); | ||
}); |
@@ -6,25 +6,33 @@ 'use strict'; | ||
test('set-defaults initialisation returns a function', (assert) => { | ||
const 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' | ||
); | ||
assert.end(); | ||
}); | ||
test('set-defaults sets a key when not present', (assert) => { | ||
const defaults = { | ||
default_val: true | ||
}; | ||
const set_defaults = set_defaults_lib(defaults); | ||
assert.isEqual(typeof set_defaults, 'function', 'Function returned after initialisation'); | ||
const actual = set_defaults({ | ||
initial: 'yes' | ||
}); | ||
const expected = { | ||
initial : 'yes', | ||
default_val: true | ||
}; | ||
test('set-defaults sets a key when not present', assert => { | ||
const defaults = { | ||
default_val: true | ||
}; | ||
const set_defaults = set_defaults_lib(defaults); | ||
assert.isEqual( | ||
typeof set_defaults, | ||
'function', | ||
'Function returned after initialisation' | ||
); | ||
const actual = set_defaults({ | ||
initial: 'yes' | ||
}); | ||
const expected = { | ||
initial: 'yes', | ||
default_val: true | ||
}; | ||
assert.isEquivalent(actual, expected, 'Defaults set where key not present'); | ||
assert.end(); | ||
assert.isEquivalent(actual, expected, 'Defaults set where key not present'); | ||
assert.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
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
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
15
2
161
110
46308
6
+ Added@hapi/address@1.0.1(transitive)
+ Addeddebug@3.2.7(transitive)
+ Addedjoi@12.1.1(transitive)
+ Addedms@2.1.3(transitive)
- Removeddebug@2.6.9(transitive)
- Removedisemail@2.2.1(transitive)
- Removeditems@2.2.1(transitive)
- Removedjoi@10.6.0(transitive)
- Removedms@2.0.0(transitive)
Updateddebug@^3.1.0
Updatedjoi@^12.0.0