Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
npm-debug.log
14 changes: 14 additions & 0 deletions src/objectUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,17 @@ export function assignDeep(target, source) {
}
return output;
}

function alphabetizeKeys(target, blacklist = []) {
const keys = Object.keys(target);

const usableKeys = keys.filter(key => !blacklist.includes(key));
usableKeys.sort();
const updated = {};
for (let i = 0; i < usableKeys.length; i++) {
updated[usableKeys[i]] = target[usableKeys[i]];
}
return updated;
}

export {alphabetizeKeys};
29 changes: 28 additions & 1 deletion src/objectUtils.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import expect from 'expect';
import {ensureArray} from './objectUtils';
import {ensureArray, alphabetizeKeys} from './objectUtils';

describe('ensureArray', () => {
it('should put object in array', () => {
Expand All @@ -16,3 +16,30 @@ describe('ensureArray', () => {
expect(ensureArray([obj])).toEqual([obj]);
});
});

describe('alphabetizeKeys', () => {
it('should alphabetize keys', () => {
const original = {
name: 'joe',
age: 35
}
const expected = {
age: 35,
name: 'joe'
}
expect(alphabetizeKeys(original)).toEqual(expected);
})

it('should remove blacklist', () => {
const original = {
name: 'joe',
ugliness: 'high',
age: 35
}
const expected = {
age: 35,
name: 'joe'
}
expect(alphabetizeKeys(original, 'ugliness')).toEqual(expected);
})
})