Last active
August 29, 2015 14:17
-
-
Save yanxi123-com/1f950b19f60138aeb433 to your computer and use it in GitHub Desktop.
formatJsonByValue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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