Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
formatJsonByValue
assert = require 'assert'
_ = require 'lodash'
formatJsonByValue = (obj) ->
return obj unless _.isArray(obj) or _.isObject(obj)
Object.keys(obj).sort().reduce (pre, key) ->
pre + formatJsonByValue obj[key]
, ''
# Test
assert.equal formatJsonByValue(
name: 'gang'
age: 25
gender: 'man'
), '25mangang', 'parse error'
assert.equal formatJsonByValue(
name: 'tg'
age: 25
teach: ['node', 'net', 'html', 'javascript']
), '25tgnodenethtmljavascript', 'parse error'
assert.equal formatJsonByValue(
name: 'tg'
teach: ['node', 'js']
city: [{
name: 'cd'
pre: 'sc'
}, {
name: 'bj'
pre: 'bj'
}]
), 'cdscbjbjtgnodejs', 'parse error'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment