Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Hookney is a helper around self referencing JSON objects for Node.js and the browser.
Hookney
is a helper around self-referencing JSON objects for Node.js and the browser.
Hookney
supports reading from and writing to files. JSON files may contain comments.
This makes Hookney
ideal for handling configuration files.
Install hookney
using npm.
npm install hookney --save
Then require it into any module.
const Hookney = require('hookney');
Hookney
has a single dependency to lodash, which must be loaded before using Hookney
.
You can download the latest release from the repository
hookney.js
unminified, including commentshookney.min.js
minified versionLoad lodash from a CDN or any other source.
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>
<!-- Alternative CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
<!-- Load from local node module -->
<script src="node_modules/lodash/lodash.min.js"></script>
Use a script tag to directly add Hookney
to the global scope.
<script src="hookney.min.js"></script>
const json = {
custom: {
tableName: 'users-table-${self:provider.stage}'
},
provider: {
stage: 'dev'
},
environment: {
USERS_TABLE: '${self:custom.tableName}'
},
configOptions: [
{
// Scenario 'high'
limit: '1mb',
strict: true,
extended: true
},
{
// Scenario 'mid'
limit: '500kb',
strict: true,
extended: false
},
{
// Scenario 'low'
limit: '10kb',
strict: false,
extended: false
}
],
config: '${self:configOptions[1]}'
};
const config = new Hookney(json).resolveReferences().json();
// => result:
config === {
custom: {
tableName: 'users-table-dev'
},
provider: {
stage: 'dev'
},
environment: {
USERS_TABLE: 'users-table-dev'
},
configOptions: [
{
limit: '1mb',
strict: true,
extended: true
},
{
limit: '500kb',
strict: true,
extended: false
},
{
limit: '10kb',
strict: false,
extended: false
}
],
config: {
limit: '500kb',
strict: true,
extended: false
}
}
const json = { a: 1, b: "str", c: '${self:a}' };
const config = Hookney.from(json).resolveReferences().json();
// is equivalent to:
const config = new Hookney(json).resolveReferences().json();
// => config === { a: 1, b: "str", c: 1 }
// Given JSON object is not touched, instead a deep clone is created.
// => json === { a: 1, b: "str", c: '${self:a}' }
const text = '{ "a": 1, "b": "str", "c": "${self:a}" }';
const config = Hookney.fromString(text).resolveReferences().json();
// => config === { a: 1, b: "str", c: 1 }
// Synchronous
const config = Hookney.fromFileSync("/path/to/file.json").resolveReferences().json();
// Asynchronous
Hookney.fromFile("/path/to/file.json", function(err, hookney)
{
if (err)
{
// Handle error.
return;
}
const config = hookney.resolveReferences().json();
});
Hookney.fromFile()
and Hookney.fromFileSync()
support an optional options
parameter.
const options = {
encoding: 'utf8', // Default encoding
flag: 'r', // Default flag
reviver: null // Default is no reviver
};
hookney.fromFileSync("/path/to/file.json", options);
Hookney.fromFile("/path/to/file.json", options, function(err, hookney)
{
});
For details on the options
parameter, please refer to the
Node.js documentation.
In addition to the options described there, 1 additional parameter reviver
is supported.
Please refer to the
JSON.parse() documentation
for details.
Hookney.fromFile()
and Hookney.fromFileSync()
are not available in the browser.
const hookney = new Hookney({ a: 1, b: "str", c: true });
// Synchronous
hookney.writeFileSync("/path/to/file.json");
// Asynchronous
Hookney.writeFile("/path/to/file.json", function(err)
{
if (err)
{
// Handle error.
}
});
writeFile()
and writeFileSync()
support an optional options
parameter.
const options = {
encoding: 'utf8', // Default encoding
mode: 0o666, // Default mode
flag: 'w', // Default flag
replacer: null, // Default is no replacer
space: null // Default is no space
};
hookney.writeFileSync("/path/to/file.json", options);
Hookney.writeFile("/path/to/file.json", options, function(err)
{
});
For details on the options
parameter, please refer to the
Node.js documentation.
In addition to the options described there, 2 additional parameters replacer
and space
are supported.
Please refer to the
JSON.stringify() documentation
for details.
writeFile()
and writeFileSync()
are not available in the browser.
Please refer to the test spec for more examples.
We use
Steps to be taken
npm install
to install all development dependencies.npm runt lint
to run static code analysis.npm test
to run the tests.npm run coverage
to track test coverage.npm run build
in project directory to build hookney.min.js
from hookney.js
.Please use Github issues for requests.
Pull requests are welcome.
We use GitHub issues to track bugs. Please ensure your bug description is clear and has sufficient instructions to be able to reproduce the issue.
The absolute best way to report a bug is to submit a pull request including a new failing test which describes the bug. When the bug is fixed, your pull request can then be merged.
The next best way to report a bug is to provide a reduced test case on jsFiddle or jsBin or produce exact code inline in the issue which will reproduce the bug.
v1.2.0
v1.1.4
v1.1.3
v1.1.2
v1.1.0
v1.0.1
v1.0.0
Copyright (c) 2016-present, Belexos. Hookney
is licensed under the
MIT License.
FAQs
Hookney is a helper around self referencing JSON objects for Node.js and the browser.
We found that hookney demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.