8000 safe-set sorting bug · Issue #559 · angus-c/just · GitHub
[go: up one dir, main page]

Skip to content

safe-set sorting bug #559

Open
Open

Description

@NilsJacobsen

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0