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

Add transformFirst option to allow modification of key to provide first key capability #107

Open
jottinger opened this issue Aug 4, 2020 · 1 comment

Comments

@jottinger
Copy link

I will be submitting a PR momentarily.

Example code for flatten, including CURRENT transformKey:

  test('Transformed Keys', function () {
    assert.deepStrictEqual(flatten({
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    }, {
      transformKey: function (key) {
        return '__' + key + '__'
      }
    }), {
      '__hello__.__world__.__again__': 'good morning',
      '__lorem__.__ipsum__.__dolor__': 'good evening'
    })
  })

  test('Transformed First Key', function () {
    assert.deepStrictEqual(flatten({
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    }, {
      transformFirst: function (key) {
        return '__' + key + '__'
      }
    }), {
      '__hello__.world.again': 'good morning',
      '__lorem__.ipsum.dolor': 'good evening'
    })
  })

  test('Transformed First Key with Custom Delimiter', function () {
    assert.deepStrictEqual(flatten({
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    }, {
      transformFirst: function (key) {
        return '__' + key + '__'
      },
      delimiter: ':'
    }), {
      '__hello__:world:again': 'good morning',
      '__lorem__:ipsum:dolor': 'good evening'
    })
  })

  test('Transformed First Key to Add Prefix with Default Delimiter', function () {
    assert.deepStrictEqual(flatten({
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    }, {
      transformFirst: function (key, delim) {
        return 'foo' + delim + key
      }
    }), {
      'foo.hello.world.again': 'good morning',
      'foo.lorem.ipsum.dolor': 'good evening'
    })
  })


  test('Transformed First Key to Add Prefix with Custom Delimiter', function () {
    assert.deepStrictEqual(flatten({
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    }, {
      transformFirst: function (key, delim) {
        return 'foo' + delim + key
      },
      delimiter: ':'
    }), {
      'foo:hello:world:again': 'good morning',
      'foo:lorem:ipsum:dolor': 'good evening'
    })
  })


  test('Transformed Results and Keys', function () {
    assert.deepStrictEqual(flatten({
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    }, {
      transformKey: function (key) {
        return '__' + key + '__'
      },
      transformFirst: function (key, delim) {
        return 'translated' + delim + key
      }
    }), {
      'translated.__hello__.__world__.__again__': 'good morning',
      'translated.__lorem__.__ipsum__.__dolor__': 'good evening'
    })
  })

Example code for unflatten:

    assert.deepStrictEqual(unflatten({
      'foo.hello.world.again': 'good morning',
      'foo.lorem.ipsum.dolor': 'good evening'
    }, {
      transformFirst: function (key, delim) {
        return key.substring(('foo'+delim).length)
      }
    }), {
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    })
  })


  test('Transformed First Key and Subsequent Keys', function () {
    assert.deepStrictEqual(unflatten({
      'foo.__hello__.__world__.__again__': 'good morning',
      'foo.__lorem__.__ipsum__.__dolor__': 'good evening'
    }, {
      transformKey: function (key) {
        return key.substring(2, key.length - 2)
      },
      transformFirst: function (key, delim) {
        return key.substring(('foo'+delim).length)
      }
    }), {
      hello: {
        world: {
          again: 'good morning'
        }
      },
      lorem: {
        ipsum: {
          dolor: 'good evening'
        }
      }
    })
  })
jottinger added a commit to Delos-tech/flat that referenced this issue Aug 4, 2020
@vandres
Copy link

vandres commented Jul 13, 2022

I really could need this feature. Maybe a improvement could be, to extend transformKey, to provide the depth in the callback. Then we could just react to "depth === 0"

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

2 participants