JSPM

smart-deep-sort

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3086
  • Score
    100M100P100Q118261F
  • License MIT

Deep sort an object, no matter what the contents are.

Package Exports

  • smart-deep-sort

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (smart-deep-sort) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

smart-deep-sort

Deep sort an object, no matter what the contents are.

CircleCI Coverage Status

Install

npm install smart-deep-sort

Usage

var sort = require("smart-deep-sort")

var mixedTypes = {
  primativeInt: 2,
  primativeString: "1",
  mixedArray: [
    {
      nestedObjName: "Nestle",
      abilities: ["rock", "and", "roll"]
    },
    [4, 1, 2, "two", "twenty-thousand"],
    "basicString"
  ]
}

var sortedMixedTypes = {
  mixedArray: [
    [1, 2, 4, "twenty-thousand", "two"],
    {
      abilities: ["and", "rock", "roll"],
      nestedObjName: "Nestle"
    },
    "basicString"
  ],
  primativeInt: 2,
  primativeString: "1"
}

var ret = sort(mixedTypes)
console.log(JSON.stringify(ret) === JSON.stringify(sortedMixedTypes))

The Rules

  • Objects fields are deep sorted by key using deep-sort-object
    • keys at all levels are sorted using default string Unicode code sort order
  • Arrays elements are sorted by type, ordered on the constructor name. Arrays come first then Booleans, etc.
    • Nested objects are sorted by using sorty to order them by keys and values.
    • All other nested object types are sorted by their contents using array-sort

Limitations

  • Cannot handle objects with undefined keys, they will probably be dropped from the resulting object.
  • Not optimized, I don't recommend using this as part of stream processing.
  • Does not handle Date types. The default string representation of the date will be used in comparisons.