Skip to content

Instantly share code, notes, and snippets.

@imyelo
Last active August 29, 2015 13:57
Show Gist options
  • Save imyelo/9499941 to your computer and use it in GitHub Desktop.
Save imyelo/9499941 to your computer and use it in GitHub Desktop.
sort by key capital
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;
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