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

[#255] Add mapKeysWith and camelCaseKeys #258

Merged
merged 11 commits into from
Feb 16, 2023
Merged

Conversation

jacob-alford
Copy link
Owner

@jacob-alford jacob-alford commented Feb 11, 2023

Closes #255

Introduces a powerful type operator and utility function for transforming a struct definition's output keys with a mapping function. And adds a camelcase keys utility.

import * as E from 'fp-ts/Either'
import { getDecoder } from 'schemata-ts/Decoder'
import * as S from 'schemata-ts/schemata'
import * as s from 'schemata-ts/struct'

const camelFromMixed = s.camelCaseKeys(
  s.defineStruct({
    foo_bar: s.required(S.String),
    'bar-qux': s.optional(S.Number),
    'foo.baz-dan': s.optional(S.Boolean),
  }),
)

const DecoderMapMixedToCamel = getDecoder(S.StructM(camelFromMixed))

assert.deepStrictEqual(
  DecoderMapMixedToCamel.decode({
    foo_bar: 'foo',
    'bar-qux': 1,
    'foo.baz-dan': true,
  }),
  E.right({ fooBar: 'foo', barQux: 1, fooBazDan: true }),
)

@jacob-alford jacob-alford added this to the 1.x milestone Feb 11, 2023
@coveralls
Copy link
Collaborator

coveralls commented Feb 11, 2023

Pull Request Test Coverage Report for Build 4194677949

  • 67 of 67 (100.0%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 100.0%

Totals Coverage Status
Change from base Build 4179116618: 0.0%
Covered Lines: 2307
Relevant Lines: 2307

💛 - Coveralls

@jacob-alford jacob-alford marked this pull request as draft February 11, 2023 22:42
@jacob-alford jacob-alford changed the title [#255] Add remapKeysWith and camelCaseKeys [#255] Add mapKeysWith and camelCaseKeys Feb 11, 2023
@jacob-alford jacob-alford force-pushed the ja/255/key-remaps branch 3 times, most recently from 7cfd9eb to b40567e Compare February 15, 2023 23:03
@jacob-alford jacob-alford marked this pull request as ready for review February 15, 2023 23:05
@jacob-alford jacob-alford merged commit 371d150 into main Feb 16, 2023
@jacob-alford jacob-alford deleted the ja/255/key-remaps branch February 16, 2023 14:02
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

Successfully merging this pull request may close these issues.

StructM Key Remap Functions
2 participants