Comparing version
@@ -1,38 +0,60 @@ | ||
// Generated by CoffeeScript 2.6.1 | ||
var json2php; | ||
// Generated by CoffeeScript 2.7.0 | ||
var json2php, make, | ||
hasProp = {}.hasOwnProperty; | ||
json2php = function(obj) { | ||
var i, result; | ||
switch (Object.prototype.toString.call(obj)) { | ||
case '[object Null]': | ||
result = 'null'; | ||
break; | ||
case '[object Undefined]': | ||
result = 'null'; | ||
break; | ||
case '[object String]': | ||
result = "'" + obj.replace(/\\/g, '\\\\').replace(/\'/g, "\\'") + "'"; | ||
break; | ||
case '[object Number]': | ||
case '[object Boolean]': | ||
result = obj.toString(); | ||
break; | ||
case '[object Array]': | ||
result = 'array(' + obj.map(json2php).join(', ') + ')'; | ||
break; | ||
case '[object Object]': | ||
result = []; | ||
for (i in obj) { | ||
if (obj.hasOwnProperty(i)) { | ||
result.push(json2php(i) + " => " + json2php(obj[i])); | ||
} | ||
make = function({linebreak = '', indent = ''} = {}) { | ||
var nest, transform; | ||
nest = { | ||
'[object Array]': function(obj, parentIndent) { | ||
var i, len, results, value; | ||
results = []; | ||
for (i = 0, len = obj.length; i < len; i++) { | ||
value = obj[i]; | ||
results.push(transform(value, parentIndent)); | ||
} | ||
result = "array(" + result.join(", ") + ")"; | ||
break; | ||
default: | ||
result = 'null'; | ||
} | ||
return result; | ||
return results; | ||
}, | ||
'[object Object]': function(obj, parentIndent) { | ||
var key, results, value; | ||
results = []; | ||
for (key in obj) { | ||
if (!hasProp.call(obj, key)) continue; | ||
value = obj[key]; | ||
results.push(transform(key, parentIndent) + ' => ' + transform(value, parentIndent)); | ||
} | ||
return results; | ||
} | ||
}; | ||
return transform = function(obj, parentIndent = '') { | ||
var items, nestIndent, objType, result; | ||
objType = Object.prototype.toString.call(obj); | ||
switch (objType) { | ||
case '[object Null]': | ||
case '[object Undefined]': | ||
result = 'null'; | ||
break; | ||
case '[object String]': | ||
result = "'" + obj.replace(/\\/g, '\\\\').replace(/\'/g, "\\'") + "'"; | ||
break; | ||
case '[object Number]': | ||
case '[object Boolean]': | ||
result = obj.toString(); | ||
break; | ||
case '[object Array]': | ||
case '[object Object]': | ||
nestIndent = parentIndent + indent; | ||
items = nest[objType](obj, nestIndent); | ||
result = `array(${linebreak + nestIndent}${items.join(',' + (linebreak === '' ? ' ' : linebreak + nestIndent))}${linebreak + parentIndent})`; | ||
break; | ||
default: | ||
result = 'null'; | ||
} | ||
return result; | ||
}; | ||
}; | ||
json2php = make(); | ||
json2php.make = make; | ||
if (typeof module !== 'undefined' && module.exports) { | ||
@@ -39,0 +61,0 @@ module.exports = json2php; |
{ | ||
"name": "json2php", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "JSON to PHP converter", | ||
@@ -31,4 +31,4 @@ "main": "lib/json2php.js", | ||
"devDependencies": { | ||
"mocha": "^9", | ||
"coffeescript": "^2" | ||
"coffeescript": "^2", | ||
"mocha": "^9" | ||
}, | ||
@@ -35,0 +35,0 @@ "license": "BSD", |
@@ -71,2 +71,22 @@ json2php | ||
### Pretty printing | ||
Create custom 'printers' with `json2php.make`: | ||
```javascript | ||
const printer = json2php.make({linebreak:'\n', indent:'\t'}) | ||
printer({one: 3, two: 20, three: [9, 3, 2]}) | ||
/* result:. | ||
array( | ||
'one' => 3, | ||
'two' => 20, | ||
'set' => array( | ||
9, | ||
3, | ||
2 | ||
) | ||
) | ||
*/ | ||
``` | ||
### For Contributors | ||
@@ -92,2 +112,5 @@ | ||
#### 0.0.6 | ||
* Add pretty print capability via `json2php.make` (thanks to @stokesman) | ||
#### 0.0.5 | ||
@@ -97,3 +120,2 @@ * Update and clean up (thanks to @SumoTTo) | ||
#### 0.0.4 | ||
@@ -107,3 +129,3 @@ * Fix for single quotes escaping (thanks to @ksky521) | ||
#### 0.0.2 | ||
* Adding the package.json to Git repository, also package dependancy | ||
* Adding the package.json to Git repository, also package dependency | ||
* Changes into the file structure | ||
@@ -120,5 +142,1 @@ * Adding CoffeeScript source ( Not finished yet ) | ||
* Added json2php into the global scope with global.json2php | ||
[](https://bitdeli.com/free "Bitdeli Badge") | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
8740
35.95%60
50%138
15%