0.3.3 • Published 7 years ago

sort-object-properties v0.3.3

Weekly downloads
95
License
MIT
Repository
github
Last release
7 years ago

sort-object-properties

Utility that return copy of object with sorted keys

Installation

npm install sort-object-properties --save

Usage

Import

//ES6
import sort from 'sort-object-properties';
//CommonJS
var sort = require('sort-object-properties');

Simple call

const objectWithUnsortedKeys = {
    d: 'value1',
    c: 'value2',
    e: 'value3',
    b: 'value4',
    a: 'value5'
};

var sortedObject = sort(objectToSort);

console.log(sortedObject); 
/*
    a: 'value5',
    b: 'value4',
    c: 'value2',
    d: 'value1',
    e: 'value3'
*/

API

Global function

By default sorts object ascending by its keys. Use second parameter if need more flexibility.

ArgumentTypeOptionalDescription
objObjectfalseObject to sort
sortFunctionSortFunctiontrueFunction used to sort object

SortFunction

ArgumentTypeOptionalDescription
aPropertyObjecttrueLeft side property
bPropertyObjecttrueRight side property

PropertyObject

PropertyTypeOptionalDescription
keystring, number, SymboltrueKey used in parent object
valueanytrueValue assigned to key

value function

Sorts object properties by its values.

ArgumentTypeOptionalDescription
objObjectfalseObject to sort
directionSortDirection, NumbertrueSort direction
valueSelectorFunctiontrueFunction that return simple value from complex type properties

key function

Sorts object properties by its keys.

ArgumentTypeOptionalDescription
objPropertyObjectfalseObject to sort
directionSortDirection, NumbertrueSort direction

sortDirection constant

Constant with sort directions, use that const or just use plain numbers in function calls.

NameValue
ascending1
descending-1

Examples

Sort by values using global function

const objectWithUnsortedValues = {
    key1: 'd',
    key2: 'a',
    key3: 'e',
    key4: 'b',
    key5: 'c'
};

//ES6 syntax
var sortedObject = sort(objectWithUnsortedValues, ({value: valueA}, {value: valueB}) => valueA > valueB);
//ES5 syntax
var sortedObject = sort(objectWithUnsortedValues, function (a, b){ return a.value > b.value;});

console.log(sortedObject);
/*
    key2: 'a',
    key4: 'b',
    key5: 'c'
    key1: 'd',
    key3: 'e',
*/

Sort by values using values function

import { value as sortByValue } from 'sort-object-properties';

const objectWithUnsortedValues = {
    key1: 'd',
    key2: 'a',
    key3: 'e',
    key4: 'b',
    key5: 'c'
};

var sortedObject = sortByValue(objectWithUnsortedValues);

console.log(sortedObject);
/*
    key2: 'a',
    key4: 'b',
    key5: 'c'
    key1: 'd',
    key3: 'e',
*/

Sort by keys using key function

import { key as sortByKey, sortDirection } from 'sort-object-properties';

const objectWithUnsortedKeys = {
    key4: 'e',
    key1: 'c',
    key3: 'c',
    key2: 'd',
    key5: 'a'
};

var sortedObject = sortByKey(objectWithUnsortedKeys, sortDirection.descending);

console.log(sortedObject);
/*
    key5: 'a',
    key4: 'e',
    key3: 'c',
    key2: 'd',
    key1: 'c'
*/
0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.3.0

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago