sortObject.js 406 B

12345678910111213141516171819202122
  1. module.exports = function sortObject (obj, keyOrder, dontSortByUnicode) {
  2. if (!obj) return
  3. const res = {}
  4. if (keyOrder) {
  5. keyOrder.forEach(key => {
  6. if (obj.hasOwnProperty(key)) {
  7. res[key] = obj[key]
  8. delete obj[key]
  9. }
  10. })
  11. }
  12. const keys = Object.keys(obj)
  13. !dontSortByUnicode && keys.sort()
  14. keys.forEach(key => {
  15. res[key] = obj[key]
  16. })
  17. return res
  18. }