Last active
August 29, 2015 13:57
-
-
Save imyelo/9499941 to your computer and use it in GitHub Desktop.
sort by key capital
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
var _ = (function () { | |
var exports = {}; | |
exports.isArray = function (obj) { | |
return obj instanceof Array; | |
}; | |
exports.each = function (list, func) { | |
var i, len; | |
if (exports.isArray(list)) { | |
for (i = 0, len = list.length; i < len; i++) { | |
func(list[i], i, list); | |
} | |
} else { | |
for (i in list) { | |
func(list[i], i, list); | |
} | |
} | |
return list; | |
}; | |
return exports; | |
})(); | |
var sortByKeyCap = function (obj) { | |
var result = {}; | |
var keys = (function () { | |
var result = []; | |
_.each(obj, function (value, key) { | |
result.push(key); | |
}); | |
return result; | |
})().sort(function (l, r) { | |
return l > r ? 1 : -1; | |
}); | |
_.each(keys, function (k) { | |
result[k] = obj[k]; | |
}); | |
return result; | |
}; | |
exports._ = _; | |
exports.sortByKeyCap = sortByKeyCap; |
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
var chai = require('chai'); | |
var expect = chai.expect; | |
var _ = require('./sortByKeyCap')._; | |
var sortByKeyCap = require('./sortByKeyCap').sortByKeyCap; | |
describe('sortByKeyCap', function () { | |
describe('base', function () { | |
describe('case', function () { | |
it('abc and abcd', function () { | |
var result = []; | |
_.each(sortByKeyCap({ | |
abc: 0, | |
abcd: 0, | |
}), function (val, key) { | |
result.push(key); | |
}); | |
expect(result).to.be.deep.equal(['abc', 'abcd']); | |
}); | |
it('abcd and abc', function () { | |
var result = []; | |
_.each(sortByKeyCap({ | |
abcd: 0, | |
abc: 0, | |
}), function (val, key) { | |
result.push(key); | |
}); | |
expect(result).to.be.deep.equal(['abc', 'abcd']); | |
}); | |
it('abcd and abc without sort', function () { | |
var result = []; | |
_.each({ | |
abcd: 0, | |
abc: 0, | |
}, function (val, key) { | |
result.push(key); | |
}); | |
expect(result).to.be.not.deep.equal(['abc', 'abcd']); | |
}); | |
it('ABC, BBB, BCD,AB, BCDEF and BCDE', function () { | |
var result = []; | |
_.each(sortByKeyCap({ | |
ABC: 0, | |
BBB: 0, | |
BCD: 0, | |
AB: 0, | |
BCDEF: 0, | |
BCDE: 0 | |
}), function (val, key) { | |
result.push(key); | |
}); | |
expect(result).to.be.deep.equal(["AB", "ABC", "BBB", "BCD", "BCDE", "BCDEF"]); | |
}); | |
it('ABC, BBB, BCD,AB, BCDEF and BCDE without sort', function () { | |
var result = []; | |
_.each({ | |
ABC: 0, | |
BBB: 0, | |
BCD: 0, | |
AB: 0, | |
BCDEF: 0, | |
BCDE: 0 | |
}, function (val, key) { | |
result.push(key); | |
}); | |
expect(result).to.be.not.deep.equal(["AB", "ABC", "BBB", "BCD", "BCDE", "BCDEF"]); | |
}); | |
it('appId, appSecret, paySign, timestamp, nonceStr', function () { | |
var result = []; | |
_.each(sortByKeyCap({ | |
appId: 'id', | |
appSecret: 'secret', | |
paySign: 'foobar', | |
timestamp: 'now', | |
nonceStr: 'xoxo' | |
}), function (val, key) { | |
result.push(key); | |
}); | |
expect(result).to.be.deep.equal(['appId', 'appSecret', 'nonceStr', 'paySign', 'timestamp']); | |
}); | |
it('appId, appSecret, paySign, timestamp, nonceStr without sort', function () { | |
var result = []; | |
_.each({ | |
appId: 'id', | |
appSecret: 'secret', | |
paySign: 'foobar', | |
timestamp: 'now', | |
nonceStr: 'xoxo' | |
}, function (val, key) { | |
result.push(key); | |
}); | |
expect(result).to.be.not.deep.equal(['appId', 'appSecret', 'nonceStr', 'paySign', 'timestamp']); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment