Skip to content

Commit

Permalink
style: refactor code for generating constants with custom labelspace
Browse files Browse the repository at this point in the history
  • Loading branch information
shixinzhu committed Aug 24, 2023
1 parent e6b9eaa commit bae0696
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
64 changes: 34 additions & 30 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,107 +1,111 @@
import { defineConstants } from './';
import { describe, it, expect } from 'vitest';
import { describe, expect, it } from 'vitest'
import { defineConstants } from './'

describe('defineConstants', () => {
it('should generate constants with default labelspace', () => {
const defs = [
{ key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
{ key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
{ key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
];
]

const constants = defineConstants(defs);
const constants = defineConstants(defs)

expect(constants.KEYS).to.deep.equal(['KEY_1', 'KEY_2', 'KEY_3']);
expect(constants.VALUES).to.deep.equal(['VALUE_1', 'VALUE_2', 'VALUE_3']);
expect(constants.KEYS).to.deep.equal(['KEY_1', 'KEY_2', 'KEY_3'])
expect(constants.VALUES).to.deep.equal(['VALUE_1', 'VALUE_2', 'VALUE_3'])
expect(constants.KV).to.deep.equal({
KEY_1: 'VALUE_1',
KEY_2: 'VALUE_2',
KEY_3: 'VALUE_3',
});
})
expect(constants.VK).to.deep.equal({
VALUE_1: 'KEY_1',
VALUE_2: 'KEY_2',
VALUE_3: 'KEY_3',
});
})
expect(constants.MAP_BY_KEY).to.deep.equal({
KEY_1: { key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
KEY_2: { key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
KEY_3: { key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
});
})
expect(constants.MAP_BY_VALUE).to.deep.equal({
VALUE_1: { key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
VALUE_2: { key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
VALUE_3: { key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
});
})
expect(constants.MAP_BY_KEY).to.deep.equal({
KEY_1: { key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
KEY_2: { key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
KEY_3: { key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
});
})
expect(constants.MAP).to.deep.equal({
KEY_1: 'VALUE_1',
KEY_2: 'VALUE_2',
KEY_3: 'VALUE_3',
});
})
expect(constants.KN).to.deep.equal({
KEY_1: 'Name 1',
KEY_2: 'Name 2',
KEY_3: 'Name 3',
});
})
expect(constants.VN).to.deep.equal({
VALUE_1: 'Name 1',
VALUE_2: 'Name 2',
VALUE_3: 'Name 3',
});
expect(constants.LIST).to.deep.equal(defs);
});
})
expect(constants.LIST).to.deep.equal(defs)
})

it('should generate constants with custom labelspace', () => {
const defs = [
{ key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
{ key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
{ key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
];
]

const constants = defineConstants(defs, 'PREFIX');
const constants = defineConstants(defs, 'PREFIX')

expect(constants.PREFIX_KEYS).to.deep.equal(['KEY_1', 'KEY_2', 'KEY_3']);
expect(constants.PREFIX_VALUES).to.deep.equal(['VALUE_1', 'VALUE_2', 'VALUE_3']);
expect(constants.PREFIX_KEYS).to.deep.equal(['KEY_1', 'KEY_2', 'KEY_3'])
expect(constants.PREFIX_VALUES).to.deep.equal([
'VALUE_1',
'VALUE_2',
'VALUE_3',
])
expect(constants.PREFIX_KV).to.deep.equal({
KEY_1: 'VALUE_1',
KEY_2: 'VALUE_2',
KEY_3: 'VALUE_3',
});
})
expect(constants.PREFIX_VK).to.deep.equal({
VALUE_1: 'KEY_1',
VALUE_2: 'KEY_2',
VALUE_3: 'KEY_3',
});
})
expect(constants.PREFIX_MAP_BY_KEY).to.deep.equal({
KEY_1: { key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
KEY_2: { key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
KEY_3: { key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
});
})
expect(constants.PREFIX_MAP_BY_VALUE).to.deep.equal({
VALUE_1: { key: 'KEY_1', value: 'VALUE_1', name: 'Name 1' },
VALUE_2: { key: 'KEY_2', value: 'VALUE_2', name: 'Name 2' },
VALUE_3: { key: 'KEY_3', value: 'VALUE_3', name: 'Name 3' },
});
})
expect(constants.PREFIX_MAP).to.deep.equal({
KEY_1: 'VALUE_1',
KEY_2: 'VALUE_2',
KEY_3: 'VALUE_3',
});
})
expect(constants.PREFIX_KN).to.deep.equal({
KEY_1: 'Name 1',
KEY_2: 'Name 2',
KEY_3: 'Name 3',
});
})
expect(constants.PREFIX_VN).to.deep.equal({
VALUE_1: 'Name 1',
VALUE_2: 'Name 2',
VALUE_3: 'Name 3',
});
expect(constants.PREFIX_LIST).to.deep.equal(defs);
});
});
})
expect(constants.PREFIX_LIST).to.deep.equal(defs)
})
})
24 changes: 12 additions & 12 deletions src/type/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ export type ToProperty<

export type ToValueKey<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleValueKey<A>
: MergeIntersection<ToSingleValueKey<A> & ToValueKey<B>>
? ToSingleValueKey<A>
: MergeIntersection<ToSingleValueKey<A> & ToValueKey<B>>
: []

export type ToValueName<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleValueName<A>
: MergeIntersection<ToSingleValueName<A> & ToValueName<B>>
? ToSingleValueName<A>
: MergeIntersection<ToSingleValueName<A> & ToValueName<B>>
: []

export type MergeIntersection<A> = A extends infer T
Expand All @@ -34,14 +34,14 @@ export type MergeIntersection<A> = A extends infer T

export type ToKeyMap<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleKeyMap<A>
: MergeIntersection<ToSingleKeyMap<A> & ToKeyMap<B>>
? ToSingleKeyMap<A>
: MergeIntersection<ToSingleKeyMap<A> & ToKeyMap<B>>
: []

export type ToValueMap<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleValueMap<A>
: MergeIntersection<ToSingleValueMap<A> & ToValueMap<B>>
? ToSingleValueMap<A>
: MergeIntersection<ToSingleValueMap<A> & ToValueMap<B>>
: []

export type ToKeys<T> = ExtractListProperty<T, 'key'>
Expand All @@ -50,12 +50,12 @@ export type ToValues<T> = ExtractListProperty<T, 'value'>

export type ToKeyValue<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleKeyValue<A>
: MergeIntersection<ToSingleKeyValue<A> & ToKeyValue<B>>
? ToSingleKeyValue<A>
: MergeIntersection<ToSingleKeyValue<A> & ToKeyValue<B>>
: []

export type ToKeyName<T> = T extends readonly [infer A, ...infer B]
? B['length'] extends 0
? ToSingleKeyName<A>
: MergeIntersection<ToSingleKeyName<A> & ToKeyName<B>>
? ToSingleKeyName<A>
: MergeIntersection<ToSingleKeyName<A> & ToKeyName<B>>
: []

0 comments on commit bae0696

Please sign in to comment.