@battis/typescript-tricks 0.7.6 → 0.7.8
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/CHANGELOG.md +14 -0
- package/dist/Record.d.ts +1 -0
- package/dist/Record.js +37 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/jest.config.ts +28 -0
- package/package.json +5 -4
- package/tests/JSONValue/JSONPrimitive.test.ts +25 -0
- package/tests/Record/isRecord.test.ts +56 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,20 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [0.7.8](https://github.com/battis/typescript-config/compare/typescript-tricks/0.7.7...typescript-tricks/0.7.8) (2026-03-06)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* improve typechecking ([af6a06e](https://github.com/battis/typescript-config/commit/af6a06ede72238bdc046dfed0dafcb33fc177fcc))
|
|
11
|
+
|
|
12
|
+
## [0.7.7](https://github.com/battis/typescript-config/compare/typescript-tricks/0.7.6...typescript-tricks/0.7.7) (2026-02-10)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Features
|
|
16
|
+
|
|
17
|
+
* isRecord<K,V>() ([fcc23b4](https://github.com/battis/typescript-config/commit/fcc23b49b0d0c2945d104f7dc88c1c902b582e0c))
|
|
18
|
+
|
|
5
19
|
## [0.7.6](https://github.com/battis/typescript-config/compare/typescript-tricks/0.7.5...typescript-tricks/0.7.6) (2026-01-04)
|
|
6
20
|
|
|
7
21
|
|
package/dist/Record.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isRecord<K extends string | number | symbol, V>(obj: unknown, isK: (k: unknown) => k is K, isV: (v: unknown) => v is V): obj is Record<K, V>;
|
package/dist/Record.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isRecord = isRecord;
|
|
4
|
+
function isRecord(obj, isK, isV) {
|
|
5
|
+
return (typeof obj === 'object' &&
|
|
6
|
+
obj !== null &&
|
|
7
|
+
Object.keys(obj).reduce((result, key) => result && isK(key), true) &&
|
|
8
|
+
Object.keys(obj)
|
|
9
|
+
.map((key) => obj[key])
|
|
10
|
+
.reduce((result, value) => result && isV(value), true) &&
|
|
11
|
+
[
|
|
12
|
+
Function,
|
|
13
|
+
Boolean,
|
|
14
|
+
Error,
|
|
15
|
+
Number,
|
|
16
|
+
Date,
|
|
17
|
+
String,
|
|
18
|
+
RegExp,
|
|
19
|
+
Array,
|
|
20
|
+
Uint8Array,
|
|
21
|
+
Uint16Array,
|
|
22
|
+
Uint32Array,
|
|
23
|
+
Int16Array,
|
|
24
|
+
Int32Array,
|
|
25
|
+
Float32Array,
|
|
26
|
+
Float64Array,
|
|
27
|
+
Map,
|
|
28
|
+
Set,
|
|
29
|
+
WeakMap,
|
|
30
|
+
WeakSet,
|
|
31
|
+
ArrayBuffer,
|
|
32
|
+
DataView,
|
|
33
|
+
Promise,
|
|
34
|
+
DisposableStack,
|
|
35
|
+
AsyncDisposableStack
|
|
36
|
+
].reduce((result, T) => result && !(obj instanceof T), true));
|
|
37
|
+
}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -25,5 +25,6 @@ __exportStar(require("./Mixin"), exports);
|
|
|
25
25
|
__exportStar(require("./OneOf"), exports);
|
|
26
26
|
__exportStar(require("./OptionalProperty"), exports);
|
|
27
27
|
__exportStar(require("./PropertyRequirements"), exports);
|
|
28
|
+
__exportStar(require("./Record"), exports);
|
|
28
29
|
__exportStar(require("./Shorthand"), exports);
|
|
29
30
|
__exportStar(require("./StaticImplements"), exports);
|
package/jest.config.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Config } from 'jest';
|
|
2
|
+
import { createDefaultEsmPreset } from 'ts-jest';
|
|
3
|
+
|
|
4
|
+
const presetConfig = createDefaultEsmPreset({});
|
|
5
|
+
|
|
6
|
+
export default {
|
|
7
|
+
...presetConfig,
|
|
8
|
+
testEnvironment: 'node',
|
|
9
|
+
testMatch: [
|
|
10
|
+
'**/tests/**/*.test.ts',
|
|
11
|
+
'**/__tests__/**/*.test.ts',
|
|
12
|
+
'**/?(*.)+(spec|test).ts'
|
|
13
|
+
],
|
|
14
|
+
moduleNameMapper: {
|
|
15
|
+
'^(\\.{1,2}/.*)\\.js$': '$1'
|
|
16
|
+
},
|
|
17
|
+
testTimeout: 30000,
|
|
18
|
+
transform: {
|
|
19
|
+
'^.+\\.tsx?$': [
|
|
20
|
+
'ts-jest',
|
|
21
|
+
{
|
|
22
|
+
diagnostics: {
|
|
23
|
+
ignoreCodes: [151002]
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
]
|
|
27
|
+
}
|
|
28
|
+
} satisfies Config;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@battis/typescript-tricks",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.8",
|
|
4
4
|
"description": "A handful of useful types and operators",
|
|
5
5
|
"homepage": "https://github.com/battis/typescript-config/tree/main/packages/typescript-tricks#readme",
|
|
6
6
|
"repository": {
|
|
@@ -17,9 +17,12 @@
|
|
|
17
17
|
"types": "./dist/index.d.ts",
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"@tsconfig/recommended": "^1.0.13",
|
|
20
|
+
"@types/jest": "^30.0.0",
|
|
20
21
|
"commit-and-tag-version": "^12.6.1",
|
|
21
22
|
"del-cli": "^6.0.0",
|
|
23
|
+
"jest": "^30.2.0",
|
|
22
24
|
"npm-run-all": "^4.1.5",
|
|
25
|
+
"ts-jest": "^29.4.6",
|
|
23
26
|
"typescript": "^5.9.3"
|
|
24
27
|
},
|
|
25
28
|
"engines": {
|
|
@@ -29,8 +32,6 @@
|
|
|
29
32
|
"build": "run-s build:*",
|
|
30
33
|
"build:clean": "del ./dist/*",
|
|
31
34
|
"build:compile": "tsc",
|
|
32
|
-
"
|
|
33
|
-
"release:build": "run-s build",
|
|
34
|
-
"release:commit": "commit-and-tag-version"
|
|
35
|
+
"version": "commit-and-tag-version"
|
|
35
36
|
}
|
|
36
37
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { JSONPrimitive } from '../../src/JSONValue.js';
|
|
2
|
+
|
|
3
|
+
export function isJSONPrimitive(obj: unknown): obj is JSONPrimitive {
|
|
4
|
+
return (
|
|
5
|
+
obj === null ||
|
|
6
|
+
typeof obj === 'number' ||
|
|
7
|
+
typeof obj === 'string' ||
|
|
8
|
+
typeof obj === 'string'
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
test('truthy', () => {
|
|
13
|
+
expect(isJSONPrimitive(null)).toBeTruthy();
|
|
14
|
+
expect(isJSONPrimitive(123)).toBeTruthy();
|
|
15
|
+
expect(isJSONPrimitive(3.14159)).toBeTruthy();
|
|
16
|
+
expect(isJSONPrimitive('foo bar')).toBeTruthy();
|
|
17
|
+
expect(isJSONPrimitive(`interpolate ${new Date()}`)).toBeTruthy();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('falsy', () => {
|
|
21
|
+
expect(isJSONPrimitive(undefined)).toBeFalsy();
|
|
22
|
+
expect(isJSONPrimitive([1, 2, 3])).toBeFalsy();
|
|
23
|
+
expect(isJSONPrimitive({ foo: 'bar' })).toBeFalsy();
|
|
24
|
+
expect(isJSONPrimitive(new Date())).toBeFalsy();
|
|
25
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { JSONPrimitive } from '../../src/JSONValue.js';
|
|
2
|
+
import { isRecord } from '../../src/Record.js';
|
|
3
|
+
import { isJSONPrimitive } from '../JSONValue/JSONPrimitive.test.js';
|
|
4
|
+
|
|
5
|
+
function isKey(obj: unknown): obj is keyof object {
|
|
6
|
+
return (
|
|
7
|
+
typeof obj === 'string' ||
|
|
8
|
+
typeof obj === 'number' ||
|
|
9
|
+
typeof obj === 'symbol'
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isUnknown(obj: unknown): obj is unknown {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isString(obj: unknown): obj is string {
|
|
18
|
+
return typeof obj === 'string';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
test('Record<string,string>', () => {
|
|
22
|
+
expect(
|
|
23
|
+
isRecord<string, string>({ foo: 'bar' }, isString, isString)
|
|
24
|
+
).toBeTruthy();
|
|
25
|
+
expect(
|
|
26
|
+
isRecord<string, string>({ foo: 123 }, isString, isString)
|
|
27
|
+
).toBeFalsy();
|
|
28
|
+
expect(isRecord<string, string>(new Date(), isString, isString)).toBeFalsy();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('Record<string,JSONPrimitive>', () => {
|
|
32
|
+
expect(
|
|
33
|
+
isRecord<string, JSONPrimitive>({ foo: 'bar' }, isString, isJSONPrimitive)
|
|
34
|
+
).toBeTruthy();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('built-in objects', () => {
|
|
38
|
+
expect(
|
|
39
|
+
isRecord<keyof object, unknown>(new String('foo bar'), isKey, isUnknown)
|
|
40
|
+
).toBeFalsy();
|
|
41
|
+
expect(
|
|
42
|
+
isRecord<keyof object, unknown>(new Number(123), isKey, isUnknown)
|
|
43
|
+
).toBeFalsy();
|
|
44
|
+
expect(
|
|
45
|
+
isRecord<keyof object, unknown>(Math.PI, isKey, isUnknown)
|
|
46
|
+
).toBeFalsy();
|
|
47
|
+
expect(
|
|
48
|
+
isRecord<keyof object, unknown>(new Uint8Array(0), isKey, isUnknown)
|
|
49
|
+
).toBeFalsy();
|
|
50
|
+
expect(
|
|
51
|
+
isRecord<keyof object, unknown>(new Map(), isKey, isUnknown)
|
|
52
|
+
).toBeFalsy();
|
|
53
|
+
expect(
|
|
54
|
+
isRecord<keyof object, unknown>(new Date(), isKey, isUnknown)
|
|
55
|
+
).toBeFalsy();
|
|
56
|
+
});
|