type-plus 4.4.2 → 4.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/cjs/object/index.d.ts +1 -0
- package/cjs/object/index.js +1 -0
- package/cjs/object/index.js.map +1 -1
- package/cjs/object/record.d.ts +6 -0
- package/cjs/object/record.js +12 -0
- package/cjs/object/record.js.map +1 -0
- package/esm/object/index.js +1 -0
- package/esm/object/index.js.map +1 -1
- package/esm/object/record.js +8 -0
- package/esm/object/record.js.map +1 -0
- package/package.json +1 -1
- package/ts/object/index.ts +1 -0
- package/ts/object/record.spec.ts +54 -0
- package/ts/object/record.ts +10 -0
package/README.md
CHANGED
|
@@ -429,6 +429,7 @@ So you may encounter some weird behavior if your logic is complex.
|
|
|
429
429
|
- `isSystemError(code, err)`: type guard `err` with Nodejs error code.
|
|
430
430
|
- `omit(obj, ...props)`: omit properties from `obj`.
|
|
431
431
|
- `pick(obj, ...props)`: pick properties from `obj`.
|
|
432
|
+
- `record<K, V>(value?)`: create a `Record<K, V>` without extra object prototype.
|
|
432
433
|
- `required(...)`: merge options and removing `Partial<T>`. From [`unpartial`](https://github.com/unional/unpartial)
|
|
433
434
|
- `requiredDeep(...)`: merge options deeply and removing `Partial<T>`. From [`unpartial`](https://github.com/unional/unpartial)
|
|
434
435
|
- `split(target, ...splitters)`: split one object into multiple objects.
|
package/cjs/object/index.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export * from './omit';
|
|
|
21
21
|
export type { OptionalKeys } from './OptionalKeys';
|
|
22
22
|
export type { PartialExcept, PartialOmit, PartialPick } from './Partial';
|
|
23
23
|
export * from './pick';
|
|
24
|
+
export * from './record';
|
|
24
25
|
export type { RecursiveIntersect } from './RecursiveIntersect';
|
|
25
26
|
export type { RecursivePartial } from './RecursivePartial';
|
|
26
27
|
export type { RecursiveRequired } from './RecursiveRequired';
|
package/cjs/object/index.js
CHANGED
|
@@ -26,6 +26,7 @@ __exportStar(require("./mapKey"), exports);
|
|
|
26
26
|
__exportStar(require("./mapProperties"), exports);
|
|
27
27
|
__exportStar(require("./omit"), exports);
|
|
28
28
|
__exportStar(require("./pick"), exports);
|
|
29
|
+
__exportStar(require("./record"), exports);
|
|
29
30
|
__exportStar(require("./reduceKey"), exports);
|
|
30
31
|
__exportStar(require("./replaceProperty"), exports);
|
|
31
32
|
__exportStar(require("./someKey"), exports);
|
package/cjs/object/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/object/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,6CAA0B;AAE1B,2CAAwB;AACxB,8CAA2B;AAC3B,4CAAyB;AACzB,+CAA4B;AAC5B,6CAA0B;AAC1B,2CAAwB;AACxB,gDAA6B;AAO7B,2CAAwB;AACxB,kDAA+B;AAC/B,yCAAsB;AAGtB,yCAAsB;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/object/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,6CAA0B;AAE1B,2CAAwB;AACxB,8CAA2B;AAC3B,4CAAyB;AACzB,+CAA4B;AAC5B,6CAA0B;AAC1B,2CAAwB;AACxB,gDAA6B;AAO7B,2CAAwB;AACxB,kDAA+B;AAC/B,yCAAsB;AAGtB,yCAAsB;AACtB,2CAAwB;AAIxB,8CAA2B;AAC3B,oDAAiC;AAGjC,4CAAyB;AACzB,0CAAuB;AAEvB,6DAA0C"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.record = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a `Record<Key, Value>`
|
|
6
|
+
*/
|
|
7
|
+
function record(value) {
|
|
8
|
+
var r = Object.create(null);
|
|
9
|
+
return value ? Object.assign(r, value) : r;
|
|
10
|
+
}
|
|
11
|
+
exports.record = record;
|
|
12
|
+
//# sourceMappingURL=record.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.js","sourceRoot":"","sources":["../../ts/object/record.ts"],"names":[],"mappings":";;;AAGA;;GAEG;AACH,SAAgB,MAAM,CAAwB,KAAoB;IAChE,IAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAC5C,CAAC;AAHD,wBAGC"}
|
package/esm/object/index.js
CHANGED
package/esm/object/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/object/index.ts"],"names":[],"mappings":"AAEA,cAAc,YAAY,CAAA;AAE1B,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,eAAe,CAAA;AAO7B,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,QAAQ,CAAA;AAGtB,cAAc,QAAQ,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/object/index.ts"],"names":[],"mappings":"AAEA,cAAc,YAAY,CAAA;AAE1B,cAAc,UAAU,CAAA;AACxB,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,UAAU,CAAA;AACxB,cAAc,eAAe,CAAA;AAO7B,cAAc,UAAU,CAAA;AACxB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,QAAQ,CAAA;AAGtB,cAAc,QAAQ,CAAA;AACtB,cAAc,UAAU,CAAA;AAIxB,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AAGjC,cAAc,WAAW,CAAA;AACzB,cAAc,SAAS,CAAA;AAEvB,cAAc,4BAA4B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"record.js","sourceRoot":"","sources":["../../ts/object/record.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,MAAM,UAAU,MAAM,CAAwB,KAAoB;IAChE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC7B,OAAO,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAC5C,CAAC"}
|
package/package.json
CHANGED
package/ts/object/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ export * from './omit'
|
|
|
21
21
|
export type { OptionalKeys } from './OptionalKeys'
|
|
22
22
|
export type { PartialExcept, PartialOmit, PartialPick } from './Partial'
|
|
23
23
|
export * from './pick'
|
|
24
|
+
export * from './record'
|
|
24
25
|
export type { RecursiveIntersect } from './RecursiveIntersect'
|
|
25
26
|
export type { RecursivePartial } from './RecursivePartial'
|
|
26
27
|
export type { RecursiveRequired } from './RecursiveRequired'
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { isType, KeyTypes, record } from '..'
|
|
2
|
+
|
|
3
|
+
test('create an empty record with value default to unknown', () => {
|
|
4
|
+
const a = record()
|
|
5
|
+
a[1] = 2
|
|
6
|
+
a['a'] = 'b'
|
|
7
|
+
a[Symbol()] = true
|
|
8
|
+
isType.equal<true, Record<KeyTypes, unknown>, typeof a>()
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
test('specify key and value types', () => {
|
|
12
|
+
const a = record<string, number>()
|
|
13
|
+
// even though the key type is string,
|
|
14
|
+
// by default JavaScript will cast number index to string
|
|
15
|
+
// so TypeScript also allows it.
|
|
16
|
+
a[1] = 2
|
|
17
|
+
a['a'] = 3
|
|
18
|
+
isType.equal<true, Record<string, number>, typeof a>()
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
test('no prototype', () => {
|
|
22
|
+
const a = record()
|
|
23
|
+
expect(Object.getPrototypeOf(a)).toBeNull()
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('infer type from initial value, the key type is widen', () => {
|
|
27
|
+
const stringRecord = record({ a: 1 })
|
|
28
|
+
isType.equal<true, Record<string, number>, typeof stringRecord>()
|
|
29
|
+
|
|
30
|
+
const numberRecord = record({ 1: 3 })
|
|
31
|
+
isType.equal<true, Record<number, number>, typeof numberRecord>()
|
|
32
|
+
|
|
33
|
+
const stringConstRecord = record({ a: 1 } as const)
|
|
34
|
+
isType.equal<true, Record<string, 1>, typeof stringConstRecord>()
|
|
35
|
+
|
|
36
|
+
const stringConstRecord2 = record({ a: 1 as const })
|
|
37
|
+
isType.equal<true, Record<string, 1>, typeof stringConstRecord2>()
|
|
38
|
+
|
|
39
|
+
const stringConstRecord3 = record({ a: 1 as const, b: 'b' })
|
|
40
|
+
isType.equal<true, Record<string, 1 | string>, typeof stringConstRecord3>()
|
|
41
|
+
|
|
42
|
+
const stringConstRecord4 = record({ a: 1 as const, b: 'b' as const })
|
|
43
|
+
isType.equal<true, Record<string, 1 | 'b'>, typeof stringConstRecord4>()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('no prototype with initial value', () => {
|
|
47
|
+
const a = record({ a: 1 })
|
|
48
|
+
expect(Object.getPrototypeOf(a)).toBeNull()
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('Object.keys() gets the keys of the initial value', () => {
|
|
52
|
+
const a = record({ a: 1, b: 2 })
|
|
53
|
+
expect(Object.keys(a)).toEqual(['a', 'b'])
|
|
54
|
+
})
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { KeyTypes } from 'type-plus'
|
|
2
|
+
import { Widen } from '../utils'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a `Record<Key, Value>`
|
|
6
|
+
*/
|
|
7
|
+
export function record<K extends KeyTypes, V>(value?: Record<K, V>): Record<Widen<K>, V> {
|
|
8
|
+
const r = Object.create(null)
|
|
9
|
+
return value ? Object.assign(r, value) : r
|
|
10
|
+
}
|