Hookney
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.
Getting started
Node.js
Install hookney
using npm.
npm install hookney --save
Then require it into any module.
const Hookney = ;
Browser
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 version
Load lodash from a CDN or any other source.
<!-- Alternative CDN --> <!-- Load from local node module -->
Use a script tag to directly add Hookney
to the global scope.
Usage
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 = 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
Examples
Use given JSON object and resolve references.
const json = a: 1 b: "str" c: '${self:a}' ; const config = Hookney; // is equivalent to: const config = 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}' }
Create JSON object from text and resolve references. Text may contain comments.
const text = '{ "a": 1, "b": "str", "c": "${self:a}" }'; const config = Hookney; // => config === { a: 1, b: "str", c: 1 }
Load JSON object from a file and resolve references. JSON file may contain comments.
// Synchronousconst config = Hookney; // AsynchronousHookney;
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; 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.
Write JSON object to file.
const hookney = a: 1 b: "str" c: true ; // Synchronoushookney; // AsynchronousHookney;
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; Hookney;
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.
More examples
Please refer to the test spec for more examples.
Testing
We use
- JSHint for static code analysis.
- Jasmine testing framework for testing.
- Karma test runner for testing in the browser.
- Istanbul test coverage framework for tracking test coverage.
Steps to be taken
- Clone or download the repository.
- Change into the project directory.
- Use
npm install
to install all development dependencies. - Use
npm runt lint
to run static code analysis. - Use
npm test
to run the tests. - Use
npm run coverage
to track test coverage. - The output should display successful execution results and a code coverage map.
Build
- Clone or download the repository.
- Change into project directory.
- Use
npm run build
in project directory to buildhookney.min.js
fromhookney.js
.
Contribution
Please use Github issues for requests.
Pull requests are welcome.
Issues
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.
Support
- Send us an email: support@belexos.com
- Follow us on Twitter: @belexos
Changelog
v1.2.0
- Update npm modules.
- Update and extend test environment.
- Add static code analysis tool JSHint.
- Add Karma test runner.
- Fix JSHint issues.
- Replace uglify-js by terser for minification.
- Update README.
v1.1.4
- Update npm modules.
v1.1.3
- Update npm modules.
v1.1.2
- Update npm modules.
v1.1.0
- Support multiple arguments on instantiation.
v1.0.1
- Fix .npmignore
v1.0.0
- Initial public release
License
Copyright (c) 2016-present, Belexos. Hookney
is licensed under the
MIT License.