angus-c / just

A library of dependency-free JavaScript utilities that do just one thing.
https://anguscroll.com/just
MIT License
6.06k stars 209 forks source link

safe-set sorting bug #559

Open NilsJacobsen opened 1 year ago

NilsJacobsen commented 1 year ago

Hey folks,

I was using this library for a while in our OS project inlang no but I currently ran into an edge case that is pretty much breaking our usage for it.

Description

If I use theset function and want to set an object with numbers as keys then they get sorted instead of leaving it in the parsed sequence.

Minimal reproducable example

import safeSet from "just-safe-set";

const testData = {
    "test.test.0": "a",
    "test.test.1": "b",
    "test.test.11": "c",
    "test.test.12": "d",
    "test.test.2": "e",
    "test.test.3": "f",
    "test.test.4": "g",
    "test.test.5": "h",
    "test.test.6": "i",
    "test.test.7": "j",
    "test.test.8": "k",
    "test.test.9": "l",
}

console.log("-- before safeSet --")
console.log(testData)

const obj = {};
Object.keys(testData).map((key, index) => {
    safeSet(obj, key, testData[key]);
})

console.log("-- after safeSet --")
console.log(obj)

Logs:

-- before safeSet --
{
  'test.test.0': 'a',
  'test.test.1': 'b',
  'test.test.11': 'c',
  'test.test.12': 'd',
  'test.test.2': 'e',
  'test.test.3': 'f',
  'test.test.4': 'g',
  'test.test.5': 'h',
  'test.test.6': 'i',
  'test.test.7': 'j',
  'test.test.8': 'k',
  'test.test.9': 'l'
}
-- after safeSet --
{
  test: {
    test: {
      '0': 'a',
      '1': 'b',
      '2': 'e',
      '3': 'f',
      '4': 'g',
      '5': 'h',
      '6': 'i',
      '7': 'j',
      '8': 'k',
      '9': 'l',
      '11': 'c',
      '12': 'd'
    }
  }
}

Expected behavior

I don't want it to be sorted. Because we us it as a part of a parse and serializing step it is super important that the output is always in the same order. Like that:

-- after safeSet --
{
  test: {
    test: {
      '0': 'a',
      '1': 'b',
      '11': 'c',
      '12': 'd',
      '2': 'e',
      '3': 'f',
      '4': 'g',
      '5': 'h',
      '6': 'i',
      '7': 'j',
      '8': 'k',
      '9': 'l'
    }
  }
}

Is that behavior intended? Do you plan to change that? Otherwise we would go on with a different solution?

Thanks folks, have a nice Friday:)