Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

safe-set sorting bug #559

Open
NilsJacobsen opened this issue May 19, 2023 · 0 comments
Open

safe-set sorting bug #559

NilsJacobsen opened this issue May 19, 2023 · 0 comments

Comments

@NilsJacobsen
Copy link

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:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant