├── LICENSE ├── README.md ├── package.json ├── stubJs.html └── stubJs.js /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 me 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 all 13 | 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 THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # stubJs 2 | Js stub generator for creating **smart** dummy data 3 | 4 | ## Basic Usage 5 | 6 | ``` 7 | var data = stubJs( 'random(cat,dog,mouse,fish)', 5 ); 8 | 9 | // [ 'cat', 'fish', 'fish', 'dog', 'mouse' ]; 10 | ``` 11 | 12 | ## Objects / Arrays 13 | ``` 14 | var data = stubJs({ 15 | 16 | boolean : 'boolean', 17 | string : 'string', 18 | number : 'number', 19 | object : { number : 'number', string : 'string' }, 20 | array : ['number',10], 21 | date : 'date', 22 | color : 'color', 23 | animal : 'random(dog,cat,mouse,fish)', 24 | static : 'my static string', 25 | dynamic : 'my number is {{number(1,10)}}!' 26 | 27 | },2 ); 28 | ``` 29 | Resulting stub data : 30 | ``` 31 | [ 32 | { 33 | boolean : true, 34 | string : 'd32kj', 35 | number : 7, 36 | object : { number : 2, string : 'dsk2j' }, 37 | array : [ 1, 6, 9, 3, 6, 7, 2, 1, 6, 3 ], 38 | date : js Date object, 39 | color : '#32cc12', 40 | animal : 'dog', 41 | static : 'my static string', 42 | dynamic : 'my number is 7!' 43 | }, 44 | { 45 | boolean : false, 46 | string : '234jk', 47 | number : 2, 48 | object : { number : 1, string : '0dsal' }, 49 | array : [ 6, 2, 8, 1, 9, 1, 0, 1, 7, 7 ], 50 | date : js Date object, 51 | color : '#99aabb', 52 | animal : 'fish', 53 | static : 'my static string', 54 | dynamic : 'my number is 9!' 55 | } 56 | ] 57 | ``` 58 | 59 | ## Options 60 | 61 | * 'boolean' 62 | * 'string(length)' 63 | * 'number(min,max)' 64 | * 'object' 65 | * 'array / array of objects' 66 | * 'date(start,end)' 67 | * 'color' 68 | * 'random(list,of,available,items)' 69 | 70 | :+1: 71 | 72 | אין עוד מלבדו 73 | 74 | [![Analytics](https://ga-beacon.appspot.com/UA-31346292-2/mooioom/stubjs)](https://github.com/mooioom/ga-beacon) 75 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stub-objects", 3 | "version": "1.0.0", 4 | "description": "Js stub generator for creating **smart** dummy data", 5 | "main": "stubJs.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/mooioom/stub-js.git" 12 | }, 13 | "author": "", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/mooioom/stub-js/issues" 17 | }, 18 | "homepage": "https://github.com/mooioom/stub-js#readme" 19 | } 20 | -------------------------------------------------------------------------------- /stubJs.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | stubJS 5 | 6 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /stubJs.js: -------------------------------------------------------------------------------- 1 | /* 2 | created by eldad levi 3 | license : free 4 | 5 | var data = stubJs({ 6 | 7 | index : 'index', 8 | boolean : 'boolean', 9 | string : 'string(length,chars)', // default 5 chars 10 | number : 'number(min,max)', // defualt 0-10 11 | object : { number : 'number', string : 'string' }, 12 | array : ['number',10], 13 | date : 'date(start,end)', // default - now 14 | color : 'color', 15 | animal : 'random(dog,cat,mouse,fish)', 16 | custom : 'my number is : {{number(1,10)}}' 17 | 18 | },2) 19 | 20 | */ 21 | let stubJs = function( settings, amount ){ 22 | 23 | var data; 24 | 25 | function parseMesulsalim(s){ 26 | if(s.indexOf('{{') != -1){ 27 | s = s.replace( /{{\s*([^}]+)\s*}}/g,function(){ 28 | var exp = arguments[0].match(/\{\{(.*)\}\}/)[1]; 29 | return stubJs(exp); 30 | }); 31 | return s; 32 | }else return s; 33 | } 34 | 35 | function parse(s,idx){ 36 | 37 | var props = typeof s == 'string' ? s.match(/\((.*)\)/) : null; if(props) props = props[1].split(','); 38 | 39 | if(typeof s == 'object' && !s.hasOwnProperty('length')) return stubJs(s); 40 | if(typeof s == 'object' && s.hasOwnProperty('length') ) return stubJs.apply(this,s); 41 | 42 | if(s.indexOf('date') === 0) return getDate.apply(this,props); 43 | if(s.indexOf('string') === 0) return getString.apply(this,props); 44 | if(s.indexOf('number') === 0) return getNumber.apply(this,props); 45 | if(s.indexOf('boolean') === 0) return getBoolean.apply(this,props); 46 | if(s.indexOf('array') === 0) return getArray.call(this,props,idx); 47 | if(s.indexOf('random') === 0) return getRandom.apply(this,props); 48 | if(s.indexOf('color') === 0) return getColor.apply(this,props); 49 | if(s.indexOf('index') === 0) return idx; 50 | 51 | s = parseMesulsalim(s); 52 | 53 | return s; 54 | 55 | } 56 | function getDate(start,end){ 57 | if(typeof start != 'undefined' && typeof end != 'undefined') return new Date(new Date(start).getTime() + Math.random() * (new Date(end).getTime() - new Date(start).getTime())); 58 | if(typeof start == 'undefined' && typeof end == 'undefined') return new Date(); 59 | } 60 | function getString(len,p) 61 | { 62 | var text = ""; var p = p || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; 63 | for( var i=0; i < (len||5); i++ ) text += p.charAt(Math.floor(Math.random() * p.length)); return text; 64 | } 65 | function getNumber(min,max){ 66 | if(typeof min != 'undefined' && typeof max != 'undefined') return Math.floor(Math.random() * (Number(max) - Number(min) + 1)) + Number(min); 67 | if(typeof min != 'undefined' && typeof max == 'undefined') return Math.floor(Math.random() * Number(min)); 68 | if(typeof min == 'undefined' && typeof max == 'undefined') return Math.floor(Math.random() * 10); 69 | } 70 | function getBoolean(pos,neg){ return Math.floor(Math.random(1)*100)>50?true:false;} 71 | function getRandom(){ if(!arguments.length) return getNumber(); return arguments[Math.floor(Math.random(1)*arguments.length)]; } 72 | function getColor(){ return '#'+Math.random().toString(16).substr(-6); } 73 | function getData( s, idx ){ var item = {}; for(var x in s) item[x] = parse(s[x],idx); return item; } 74 | function getArray( s, idx ){ return parseMesulsalim(s[idx]); } 75 | 76 | if(typeof settings == 'string') { 77 | if(typeof amount == 'undefined') return parse(settings); 78 | data = []; for(var a = 0;a