Skip to content

Commit

Permalink
updated:
Browse files Browse the repository at this point in the history
 * readme
 * variables in toPath
fixed docblock in toPath
  • Loading branch information
JPeer264 committed Dec 2, 2016
1 parent aa45585 commit 495f010
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
28 changes: 15 additions & 13 deletions lib/toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
var path = require('path');
var json = require('../index');
var includes = require('lodash.includes');
var pathArray;
var delimiterSeperatedArray;

/**
* creates the folder structure.
* creates a delimiter seperated chain of single keys/values
*
* @param {Object} json
* @param {Object} options
* @param {String} delimiter='/' the delimiter to seperate every single key/value.
*/
function toPath(_json, options, delimiter) {
pathArray = [];
delimiterSeperatedArray = [];

if (typeof options === 'string') {
delimiter = options;
options = {};
}

delimiter = delimiter || '/';
options = options || {};
delimiter = delimiter || '/';
options = options || {};
options.type = options.type || [];

if (!json.check('object', _json)) {
Expand All @@ -28,15 +30,15 @@ function toPath(_json, options, delimiter) {

_jsonConcat(_json, options, delimiter);

return pathArray
return delimiterSeperatedArray
}

/**
* Make a json file into a path.
* Make a json file into a delimiter seperated string. It will push everything into the global delimiterSeperatedArray
*
* @param _json {Objects}
*
* @return pathArray
* @param {Object} _json
* @param {Object} options
* @param {String} delimiter='/' the delimiter to seperate every single key/value.
*/
function _jsonConcat(_json, options, _delimiter, _jsonString) {
for (var key in _json) {
Expand All @@ -58,14 +60,14 @@ function _jsonConcat(_json, options, _delimiter, _jsonString) {
}

if (options.type.length === 0 || includes(options.type, typeOfString)) {
pathArray.push(jsonString);
delimiterSeperatedArray.push(jsonString);
}

if (typeOfString === 'array') {
// arrays should iterate over single values and add it to the global pathArray
// arrays should iterate over single values and add it to the global delimiterSeperatedArray
for (var i = 0; i < value.length; i++) {
if (options.type.length === 0 || includes(options.type, typeof value[i])) {
pathArray.push(jsonString + _delimiter + value[i])
delimiterSeperatedArray.push(jsonString + _delimiter + value[i])
}
}
} else if (typeOfString === 'object') {
Expand Down
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ var json = require('json-extra');

- [check](#check)
- [isJson](#check)
- [isValid](#check)
- [toPath](#toPath)
- [chain](#toPath)
- [readToObj](#readToObj)
- [readToObjSync](#readToObj)
- [create](#create)
Expand Down Expand Up @@ -63,7 +65,11 @@ json.isJson(myJsonString) // true | false

### toPath()

**toPath(json[, delimiter])**
**toPath(json[, options][, delimiter])**

Options:

- type (array | string): Get specific types. Available options: `array`, `object`, `string`, `boolean` or `number`

If you want to change your json string into a path just hit this method.
`base` in an object is always the name of the folder.
Expand Down

0 comments on commit 495f010

Please sign in to comment.