Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
zerious committed Dec 10, 2015
1 parent 85ae33d commit 23533b0
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 191 deletions.
1 change: 0 additions & 1 deletion .npmignore
Expand Up @@ -7,4 +7,3 @@
.travis.yml
.DS_Store
coverage
node_modules
3 changes: 3 additions & 0 deletions .travis.yml
@@ -1,5 +1,8 @@
language: node_js
node_js:
- "4.1"
- "4.0"
- "0.12"
- "0.10"
after_script:
- npm run coveralls
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,4 @@
# lighter-json Change Log

## v1.0.0 - TODO
* `major` Community release.
62 changes: 62 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,62 @@
# Contributing to lighter-json

We welcome contributions from the community and are happy to have them.
Please follow this guide when logging issues or making code changes.

## Logging Issues

All issues should be created using the
[new issue form](https://github.com/lighterio/lighter-json/issues/new).
Please describe the issue including steps to reproduce. Also, make sure
to indicate the version that has the issue.

## Changing Code

Code changes are welcome and encouraged! Please follow our process:

1. Fork the repository on GitHub.
2. Fix the issue ensuring that your code follows the
[style guide](http://lighter.io/style-guide).
3. Add tests for your new code, ensuring that you have 100% code coverage.
(If necessary, we can help you reach 100% prior to merging.)
* Run `npm test` to run tests quickly, without testing coverage.
* Run `npm run cover` to test coverage and generate a report.
* Run `npm run report` to open the coverage report you generated.
4. [Pull requests](http://help.github.com/send-pull-requests/) should be made
to the [master branch](https://github.com/lighterio/lighter-json/tree/master).

## Contributor Code of Conduct

As contributors and maintainers of `lighter-json`, we pledge to respect all
people who contribute through reporting issues, posting feature requests,
updating documentation, submitting pull requests or patches, and other
activities.

If any participant in this project has issues or takes exception with a
contribution, they are obligated to provide constructive feedback and never
resort to personal attacks, trolling, public or private harassment, insults, or
other unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, edits, issues, and other contributions
that are not aligned with this Code of Conduct. Project maintainers who do
not follow the Code of Conduct may be removed from the project team.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by opening an issue or contacting one or more of the project
maintainers.

We promise to extend courtesy and respect to everyone involved in this project
regardless of gender, gender identity, sexual orientation, ability or
disability, ethnicity, religion, age, location, native language, or level of
experience.

## Acknowledgements

We would like to thank all of the amazing people who use, support,
promote, enhance, document, patch, and submit comments & issues -
`lighter-json` couldn't exist without you.

Additionally, huge thanks go to [eBay](http://rover.ebay.com/rover/1/711-53200-19255-0/1?icep_ff3=9&pub=5575137484&toolid=10001&campid=5337778482&icep_uq=node.js&icep_sortBy=12&ipn=psmain&icep_vectorid=229466&kwid=902099&mtid=824&kw=lg) for employing
and supporting [`lighter-json`](http://lighter.io/lighter-json) project
maintainers, and for being an epically awesome place to work (and play).
15 changes: 15 additions & 0 deletions LICENSE.md
@@ -0,0 +1,15 @@
# ISC License

Copyright (c) 2015, Sam Eubank

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
127 changes: 21 additions & 106 deletions README.md
@@ -1,116 +1,31 @@
# `lighter-stringify`
[![NPM Version](https://img.shields.io/npm/v/lighter-stringify.svg)](https://npmjs.org/package/lighter-stringify)
[![Downloads](https://img.shields.io/npm/dm/lighter-stringify.svg)](https://npmjs.org/package/lighter-stringify)
[![Build Status](https://img.shields.io/travis/lighterio/lighter-stringify.svg)](https://travis-ci.org/lighterio/lighter-stringify)
[![Code Coverage](https://img.shields.io/coveralls/lighterio/lighter-stringify/master.svg)](https://coveralls.io/r/lighterio/lighter-stringify)
[![Dependencies](https://img.shields.io/david/lighterio/lighter-stringify.svg)](https://david-dm.org/lighterio/lighter-stringify)
[![Standard Style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard)
[![Support](https://img.shields.io/gratipay/Lighter.io.svg)](https://gratipay.com/Lighter.io/)
# lighter-json
[![Chat](https://badges.gitter.im/chat.svg)](//gitter.im/lighterio/public)
[![Version](https://img.shields.io/npm/v/lighter-json.svg)](//www.npmjs.com/package/lighter-json)
[![Downloads](https://img.shields.io/npm/dm/lighter-json.svg)](//www.npmjs.com/package/lighter-json)
[![Build](https://img.shields.io/travis/lighterio/lighter-json.svg)](//travis-ci.org/lighterio/lighter-json)
[![Coverage](https://img.shields.io/coveralls/lighterio/lighter-json/master.svg)](//coveralls.io/r/lighterio/lighter-json)
[![Style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](//www.npmjs.com/package/standard)

The `lighter-stringify` module is a lightweight circular-safe JSON stringifier.


## Installation
The `lighter-json` module is a lightweight JavaScript Object Notation utility.

## Quick Start
From your project directory, install and save as a dependency:
```bash
npm install --save lighter-stringify
npm install --save lighter-json
```


## API

For now, please see the [source](https://github.com/lighterio/lighter-stringify/blob/master/lighter-stringify.js).


## Examples

For now, please see the [tests](https://github.com/lighterio/lighter-stringify/blob/master/test/type.js).


## Acknowledgements

We would like to thank all of the amazing people who use, support,
promote, enhance, document, patch, and submit comments & issues -
`lighter-stringify` couldn't exist without you.

Additionally, huge thanks go to [Goin’](https://goin.io) for employing
and supporting [`lighter-stringify`](http://lighter.io/lighter-stringify) project
maintainers, and for being an epically awesome place to work (and play).


## MIT License

Copyright (c) 2014 Sam Eubank

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


## How to Contribute

We welcome contributions from the community and are happy to have them.
Please follow this guide when logging issues or making code changes.

### Logging Issues

All issues should be created using the
[new issue form](https://github.com/lighterio/lighter-stringify/issues/new).
Please describe the issue including steps to reproduce. Also, make sure
to indicate the version that has the issue.

### Changing Code

Code changes are welcome and encouraged! Please follow our process:

1. Fork the repository on GitHub.
2. Fix the issue ensuring that your code follows the
[style guide](http://lighter.io/style-guide).
3. Add tests for your new code, ensuring that you have 100% code coverage.
(If necessary, we can help you reach 100% prior to merging.)
* Run `npm test` to run tests quickly, without testing coverage.
* Run `npm run cover` to test coverage and generate a report.
* Run `npm run report` to open the coverage report you generated.
4. [Pull requests](http://help.github.com/send-pull-requests/) should be made
to the [master branch](https://github.com/lighterio/lighter-stringify/tree/master).

### Contributor Code of Conduct

As contributors and maintainers of `lighter-stringify`, we pledge to respect all
people who contribute through reporting issues, posting feature requests,
updating documentation, submitting pull requests or patches, and other
activities.

If any participant in this project has issues or takes exception with a
contribution, they are obligated to provide constructive feedback and never
resort to personal attacks, trolling, public or private harassment, insults, or
other unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, edits, issues, and other contributions
that are not aligned with this Code of Conduct. Project maintainers who do
not follow the Code of Conduct may be removed from the project team.
### JSON.stringify(value[, replacer[, space]])
Perform a JSON stringify operation on a value, using automatic cycle detection
if a replacer function is not provided.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by opening an issue or contacting one or more of the project
maintainers.
### JSON.decorate([json])
Decorate an object (by default the global JSON object), with the `lighter-json`
package's methods.

We promise to extend courtesy and respect to everyone involved in this project
regardless of gender, gender identity, sexual orientation, ability or
disability, ethnicity, religion, age, location, native language, or level of
experience.
## More on lighter-json...
* [Contributing](//github.com/lighterio/lighter-json/blob/master/CONTRIBUTING.md)
* [License (ISC)](//github.com/lighterio/lighter-json/blob/master/LICENSE.md)
* [Change Log](//github.com/lighterio/lighter-json/blob/master/CHANGELOG.md)
* [Roadmap](//github.com/lighterio/lighter-json/blob/master/ROADMAP.md)
9 changes: 9 additions & 0 deletions ROADMAP.md
@@ -0,0 +1,9 @@
# lighter-json Roadmap

***This is a living document. It describes priorities as they are perceived
today, and it can evolve over time.***

## Got ideas?
If you have ideas for features that should be in this roadmap, please submit
a pull request.

89 changes: 89 additions & 0 deletions lighter-json.js
@@ -0,0 +1,89 @@
// Reference the native JSON.stringify function in case we change it.
var nativeStringify = JSON.stringify

var self = module.exports = {

/**
* Stringify a value while safely detecting circularity.
*
* @param {Any} value A value to stringify.
* @param {Function} replacer An optional replacer function.
* @param {Number|String} space An optional spacer string or number of
* spaces.
* @return {String} Stringified JSON.
*/
stringify: function stringify (value, replacer, space) {
return replacer
? nativeStringify(value, replacer, space)
: safeStringify(value, [], space)
},

/**
* Decorate the JSON object (or a given object) with lighter-json methods.
*
* @param {[type]} json [description]
* @return {[type]} [description]
*/
decorate: function decorate (json) {
json = json || JSON
json.scriptify = self.scriptify
json.nativeStringify = nativeStringify
json.stringify = self.stringify
}

}

/**
* Stringify using a stack of parent values to detect circularity.
*
* @param {Any} value A value to stringify.
* @param {Array} stack An optional stack of parent values.
* @param {Number|String} space An optional spacer string or number of
* spaces.
* @return {String} Stringified JSON.
*/
function safeStringify (value, stack, space) {
if (value === null) {
return 'null'
}
if (typeof value !== 'object') {
return nativeStringify(value)
}
var length = stack.length
for (var i = 0; i < length; i++) {
if (stack[i] === value) {
return '"[Circular ' + (length - i) + ']"'
}
}
stack.push(value)
var isArray = (value instanceof Array)
var list = []
var json
if (isArray) {
length = value.length
for (i = 0; i < length; i++) {
json = safeStringify(value[i], stack, space)
if (json !== undefined) {
list.push(json)
}
}
} else {
for (var key in value) {
json = safeStringify(value[key], stack, space)
if (json !== undefined) {
key = nativeStringify(key) + ':' + (space ? ' ' : '')
list.push(key + json)
}
}
}
if (space && list.length) {
var indent = '\n' + (new Array(stack.length)).join(space)
var indentSpace = indent + space
list = indentSpace + list.join(',' + indentSpace) + indent
} else {
list = list.join(',')
}
value = isArray ? '[' + list + ']' : '{' + list + '}'
stack.pop()
return value
}

0 comments on commit 23533b0

Please sign in to comment.