type-plus 4.4.0 → 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 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.
@@ -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';
@@ -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);
@@ -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;AAItB,8CAA2B;AAC3B,oDAAiC;AAGjC,4CAAyB;AACzB,0CAAuB;AAEvB,6DAA0C"}
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,6 @@
1
+ import { KeyTypes } from 'type-plus';
2
+ import { Widen } from '../utils';
3
+ /**
4
+ * Creates a `Record<Key, Value>`
5
+ */
6
+ export declare function record<K extends KeyTypes, V>(value?: Record<K, V>): Record<Widen<K>, V>;
@@ -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"}
@@ -1,4 +1,6 @@
1
1
  import { AnyFunction } from '../function';
2
2
  import { RecursivePartial } from '../object';
3
- export declare type Stub<T> = T extends AnyFunction ? T : RecursivePartial<T>;
4
- export declare function stub<T>(stub: Stub<T>): T;
3
+ export declare namespace stub {
4
+ type Param<T> = T extends AnyFunction ? T : RecursivePartial<T>;
5
+ }
6
+ export declare function stub<T>(stub?: stub.Param<T>): T;
@@ -1 +1 @@
1
- {"version":3,"file":"stub.js","sourceRoot":"","sources":["../../ts/testing/stub.ts"],"names":[],"mappings":";;;AAKA,SAAgB,IAAI,CAAI,IAAa;IACnC,OAAO,IAAS,CAAA;AAClB,CAAC;AAFD,oBAEC"}
1
+ {"version":3,"file":"stub.js","sourceRoot":"","sources":["../../ts/testing/stub.ts"],"names":[],"mappings":";;;AAOA,SAAgB,IAAI,CAAI,IAAoB;IAC1C,OAAO,IAAS,CAAA;AAClB,CAAC;AAFD,oBAEC"}
@@ -10,6 +10,7 @@ export * from './mapKey';
10
10
  export * from './mapProperties';
11
11
  export * from './omit';
12
12
  export * from './pick';
13
+ export * from './record';
13
14
  export * from './reduceKey';
14
15
  export * from './replaceProperty';
15
16
  export * from './someKey';
@@ -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;AAItB,cAAc,aAAa,CAAA;AAC3B,cAAc,mBAAmB,CAAA;AAGjC,cAAc,WAAW,CAAA;AACzB,cAAc,SAAS,CAAA;AAEvB,cAAc,4BAA4B,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,8 @@
1
+ /**
2
+ * Creates a `Record<Key, Value>`
3
+ */
4
+ export function record(value) {
5
+ const r = Object.create(null);
6
+ return value ? Object.assign(r, value) : r;
7
+ }
8
+ //# sourceMappingURL=record.js.map
@@ -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"}
@@ -1 +1 @@
1
- {"version":3,"file":"stub.js","sourceRoot":"","sources":["../../ts/testing/stub.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,IAAI,CAAI,IAAa;IACnC,OAAO,IAAS,CAAA;AAClB,CAAC"}
1
+ {"version":3,"file":"stub.js","sourceRoot":"","sources":["../../ts/testing/stub.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,IAAI,CAAI,IAAoB;IAC1C,OAAO,IAAS,CAAA;AAClB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-plus",
3
- "version": "4.4.0",
3
+ "version": "4.5.0",
4
4
  "description": "Provides additional types for TypeScript.",
5
5
  "homepage": "https://github.com/unional/type-plus",
6
6
  "bugs": {
@@ -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
+ }
@@ -29,3 +29,10 @@ test('stub function params can be omitted', () => {
29
29
  expect(a('1', 1)).toBe(false)
30
30
  isType.equal<true, typeof a, typeof real>()
31
31
  })
32
+
33
+ test('stub input can be omitted', () => {
34
+ const real = { a: 1, b: { c: 2 }, d: 3 }
35
+
36
+ const a = stub<typeof real>()
37
+ isType.equal<true, typeof a, typeof real>()
38
+ })
@@ -1,8 +1,10 @@
1
1
  import { AnyFunction } from '../function'
2
2
  import { RecursivePartial } from '../object'
3
3
 
4
- export type Stub<T> = T extends AnyFunction ? T : RecursivePartial<T>
4
+ export namespace stub {
5
+ export type Param<T> = T extends AnyFunction ? T : RecursivePartial<T>
6
+ }
5
7
 
6
- export function stub<T>(stub: Stub<T>) {
8
+ export function stub<T>(stub?: stub.Param<T>) {
7
9
  return stub as T
8
10
  }