├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── bin └── jsonchance ├── bower.json ├── lib └── index.js ├── package.json └── test └── json-chance.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | coverage 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 0.10 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Luis Farzati 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # json-chance 2 | 3 | [![NPM version](https://badge.fury.io/js/json-chance.png)](http://badge.fury.io/js/json-chance) [![NPM downloads][npm-downloads-image]][npm-downloads-url] [![Build Status](https://secure.travis-ci.org/luisfarzati/json-chance.png)](http://travis-ci.org/luisfarzati/json-chance) [![Dependency Status](https://gemnasium.com/luisfarzati/json-chance.svg)](https://gemnasium.com/luisfarzati/json-chance) [![Donations](http://img.shields.io/gratipay/luisfarzati.svg)](https://www.gratipay.com/luisfarzati) 4 | 5 | Create random JSON objects using [json-spawn](https://github.com/luisfarzati/json-spawn) and [Chance.js](http://chancejs.com/). 6 | 7 | ### Node 8 | ```js 9 | var jsonChance = require('json-chance') 10 | 11 | jsonChance('guid,name,profile(url,twitter,fbid)') 12 | 13 | // returns 14 | // { 15 | // "guid": "55585f1d-aedf-50ef-9436-3cbeb3e76afc", 16 | // "name": "William McCoy", 17 | // "profile": { 18 | // "url": "http://bim.gov/du", 19 | // "twitter": "@nijgu", 20 | // "fbid": 1000053597266623 21 | // } 22 | // } 23 | 24 | ``` 25 | 26 | ### CLI 27 | ```bash 28 | $ jsonchance 'guid,name,profile(url,twitter,fbid)' 29 | 30 | { 31 | "guid": "55585f1d-aedf-50ef-9436-3cbeb3e76afc", 32 | "name": "William McCoy", 33 | "profile": { 34 | "url": "http://bim.gov/du", 35 | "twitter": "@nijgu", 36 | "fbid": 1000053597266623 37 | } 38 | } 39 | ``` 40 | 41 | ## Installation 42 | 43 | ```bash 44 | $ npm install json-chance 45 | ``` 46 | 47 | ## Tests 48 | 49 | ```bash 50 | $ npm test 51 | $ npm run coverage 52 | ``` 53 | 54 | ## License 55 | 56 | [MIT](LICENSE) 57 | 58 | [npm-downloads-image]: https://img.shields.io/npm/dm/json-chance.svg 59 | [npm-downloads-url]: https://npmjs.org/package/json-chance 60 | -------------------------------------------------------------------------------- /bin/jsonchance: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict' 3 | 4 | var path = require('path') 5 | , jsonChance = require('../lib/index') 6 | , myname = path.basename(__filename) 7 | , query = process.argv[2] 8 | 9 | if (!query) { 10 | console.log('\nUsage:', myname, '{query}') 11 | console.log('\nExamples:') 12 | console.log(' ', myname, '"guid,name,age"') 13 | console.log(' ', myname, '"posts{2}(id:guid,author(id:guid,name),comments{0})"') 14 | console.log('\nFor more information check out the project page (http://github.com/luisfarzati/json-chance)\n') 15 | } 16 | else { 17 | console.log(JSON.stringify(jsonChance(process.argv[2]), '', 1)) 18 | } 19 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-chance", 3 | "version": "0.1.0", 4 | "description": "Create random JSON objects using json-spawn and Chance.js", 5 | "homepage": "https://github.com/luisfarzati/json-chance", 6 | "authors": ["Luis Farzati "], 7 | "main": "lib/index.js", 8 | "keywords": ["mock", "generator", "random", "chance", "fields", "select", "query", "json"], 9 | "license": "MIT", 10 | "ignore": [ 11 | "**/.*", 12 | "node_modules", 13 | "bower_components", 14 | "test", 15 | "tests" 16 | ] 17 | } 18 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | var chance = require('chance').Chance() 2 | , spawn = require('json-spawn')(chance) 3 | 4 | module.exports = function jsonChance(query) { 5 | return spawn(query) 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "json-chance", 3 | "version": "0.1.1", 4 | "description": "Create random JSON objects using json-spawn and Chance.js", 5 | "keywords": ["mock", "generator", "random", "chance", "fields", "select", "query", "json"], 6 | "author": "lfarzati@gmail.com", 7 | "license": "MIT", 8 | "repository": { 9 | "type": "git", 10 | "url": "git://github.com/luisfarzati/json-chance.git" 11 | }, 12 | "main": "lib/index", 13 | "bin": { 14 | "jsonchance": "bin/jsonchance" 15 | }, 16 | "scripts": { 17 | "test": "mocha", 18 | "test:watch": "mocha -w -G -R min", 19 | "coverage": "istanbul cover _mocha" 20 | }, 21 | "dependencies": { 22 | "chance": "^0.7.6", 23 | "json-spawn": "^0.1.0" 24 | }, 25 | "devDependencies": { 26 | "istanbul": "^0.3.17", 27 | "mocha": "~1.12.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/json-chance.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | , jsonChance = require('../lib') 3 | 4 | describe('jsonChance', function () { 5 | it('should spawn a new object with the specified fields', function () { 6 | var r = jsonChance('user(id:guid,enabled:bool,profile(url,first,last,age,city,country,twitter,fbid,friends{5;9}(id:guid)))') 7 | 8 | assert.deepEqual(typeof r.user, 'object') 9 | assert.deepEqual(typeof r.user.id, 'string') 10 | assert.deepEqual(typeof r.user.enabled, 'boolean') 11 | assert.deepEqual(typeof r.user.profile, 'object') 12 | assert.deepEqual(typeof r.user.profile.url, 'string') 13 | assert.deepEqual(typeof r.user.profile.first, 'string') 14 | assert.deepEqual(typeof r.user.profile.last, 'string') 15 | assert.deepEqual(typeof r.user.profile.age, 'number') 16 | assert.deepEqual(typeof r.user.profile.city, 'string') 17 | assert.deepEqual(typeof r.user.profile.country, 'string') 18 | assert.deepEqual(typeof r.user.profile.twitter, 'string') 19 | assert.deepEqual(typeof r.user.profile.fbid, 'number') 20 | assert.deepEqual(typeof r.user.profile.friends, 'object') 21 | assert(r.user.profile.friends.length >= 5 && r.user.profile.friends.length <= 9) 22 | assert.deepEqual(typeof r.user.profile.friends[0].id, 'string') 23 | }) 24 | }) 25 | --------------------------------------------------------------------------------