optolith-database-schema 0.17.0 → 0.17.2
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 +11 -0
- package/lib/cache/activatableSelectOptions.js +3 -3
- package/lib/cache/magicalAndBlessedAdvantagesAndDisadvantages.js +1 -1
- package/lib/errors.js +5 -5
- package/lib/helpers/array.d.ts +1 -1
- package/lib/helpers/array.js +3 -3
- package/lib/rendering/UI.d.ts +14 -0
- package/lib/rendering/UI.js +28 -0
- package/lib/types/UI.d.ts +4 -0
- package/lib/validation/schema.js +2 -2
- package/package.json +3 -1
- package/schema/UI.schema.json +16 -0
- package/lib/helpers/nullable.d.ts +0 -35
- package/lib/helpers/nullable.js +0 -31
- package/lib/helpers/nullable.test.d.ts +0 -1
- package/lib/helpers/nullable.test.js +0 -46
- package/lib/helpers/object.d.ts +0 -5
- package/lib/helpers/object.js +0 -16
- package/lib/helpers/object.test.d.ts +0 -1
- package/lib/helpers/object.test.js +0 -15
- package/lib/helpers/typeSafety.d.ts +0 -14
- package/lib/helpers/typeSafety.js +0 -16
- package/lib/helpers/typeSafety.test.d.ts +0 -1
- package/lib/helpers/typeSafety.test.js +0 -10
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [0.17.2](https://github.com/elyukai/optolith-database-schema/compare/v0.17.1...v0.17.2) (2024-10-06)
|
|
6
|
+
|
|
7
|
+
### [0.17.1](https://github.com/elyukai/optolith-database-schema/compare/v0.17.0...v0.17.1) (2024-10-06)
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* add minimum and maximum UI strings with placeholders ([1bf8df1](https://github.com/elyukai/optolith-database-schema/commit/1bf8df1575c605b50d2990ed5cd85892d871ecba))
|
|
13
|
+
* add pluralization type definitions ([a8340e6](https://github.com/elyukai/optolith-database-schema/commit/a8340e6dd488945b63347a80de0f54f5eb5050e8))
|
|
14
|
+
* use @optolith/helpers package ([d8324f7](https://github.com/elyukai/optolith-database-schema/commit/d8324f7042f459201452704bffa3c06e1ec582fe))
|
|
15
|
+
|
|
5
16
|
## [0.17.0](https://github.com/elyukai/optolith-database-schema/compare/v0.16.8...v0.17.0) (2024-10-03)
|
|
6
17
|
|
|
7
18
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { isNotNullish } from "
|
|
2
|
-
import { mapObject } from "
|
|
3
|
-
import { assertExhaustive } from "
|
|
1
|
+
import { isNotNullish } from "@optolith/helpers/nullable";
|
|
2
|
+
import { mapObject } from "@optolith/helpers/object";
|
|
3
|
+
import { assertExhaustive } from "@optolith/helpers/typeSafety";
|
|
4
4
|
const PRINCIPLES_ID = 31;
|
|
5
5
|
const PROPERTY_KNOWLEDGE_ID = 3;
|
|
6
6
|
const ASPECT_KNOWLEDGE_ID = 1;
|
package/lib/errors.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { filterNonNullable } from "@optolith/helpers/array";
|
|
2
2
|
import { collator } from "./helpers/i18n.js";
|
|
3
3
|
/**
|
|
4
4
|
* Pretty-prints an error map.
|
|
@@ -12,11 +12,11 @@ export const printErrors = (errorsByFile, printOptions = {}) => {
|
|
|
12
12
|
return Object.entries(errorsByFile)
|
|
13
13
|
.sort(([filePathA], [filePathB]) => collator.compare(filePathA, filePathB))
|
|
14
14
|
.flatMap(verbose
|
|
15
|
-
? ([filePath, errors]) =>
|
|
15
|
+
? ([filePath, errors]) => filterNonNullable(errors.map(printVerboseError(filePath)))
|
|
16
16
|
: ([filePath, errors]) => [
|
|
17
|
-
...
|
|
17
|
+
...errors
|
|
18
18
|
.filter((error) => error.keyword === "integrity" || error.keyword === "filename")
|
|
19
|
-
.map(printVerboseError(filePath))
|
|
19
|
+
.map(printVerboseError(filePath)),
|
|
20
20
|
...(errors.some(error => error.keyword !== "integrity" && error.keyword !== "filename")
|
|
21
21
|
? [errorMessageBlock([filePath], "has schema errors")]
|
|
22
22
|
: []),
|
|
@@ -29,5 +29,5 @@ const printVerboseError = (filePath) => (error) => {
|
|
|
29
29
|
};
|
|
30
30
|
const errorMessageBlock = (path, message) => [
|
|
31
31
|
...path.map((segment, i) => `${" ".repeat(i * 2)}in "${segment}":`),
|
|
32
|
-
`${" ".repeat(path.length * 2)}${message}
|
|
32
|
+
`${" ".repeat(path.length * 2)}${message}`,
|
|
33
33
|
].join("\n");
|
package/lib/helpers/array.d.ts
CHANGED
package/lib/helpers/array.js
CHANGED
|
@@ -3,13 +3,13 @@ Object.defineProperty(Array.prototype, "promiseAll", {
|
|
|
3
3
|
writable: true,
|
|
4
4
|
value: function promiseAll() {
|
|
5
5
|
return Promise.all(this);
|
|
6
|
-
}
|
|
6
|
+
},
|
|
7
7
|
});
|
|
8
8
|
Object.defineProperty(Array.prototype, "objectFromEntries", {
|
|
9
9
|
configurable: true,
|
|
10
10
|
writable: true,
|
|
11
11
|
value: function objectFromEntries() {
|
|
12
12
|
return Object.fromEntries(this);
|
|
13
|
-
}
|
|
13
|
+
},
|
|
14
14
|
});
|
|
15
|
-
export
|
|
15
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PluralizationCategories } from "../types/_I18n.js";
|
|
2
|
+
import { UI } from "../types/UI.js";
|
|
3
|
+
declare const pluralType: {
|
|
4
|
+
[K in keyof UI as UI[K] extends PluralizationCategories ? K : never]: Intl.PluralRuleType;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* The keys of the UI strings that support pluralization categories.
|
|
8
|
+
*/
|
|
9
|
+
export type KeySupportingPluralizationCategories = keyof typeof pluralType;
|
|
10
|
+
/**
|
|
11
|
+
* Returns the plural rule type for the given key.
|
|
12
|
+
*/
|
|
13
|
+
export declare const getPluralRuleTypeForKey: (key: KeySupportingPluralizationCategories) => Intl.PluralRuleType;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const pluralType = {
|
|
2
|
+
"{0} Adventure Points": "cardinal",
|
|
3
|
+
"You are missing {0} Adventure Points to do this.": "cardinal",
|
|
4
|
+
"since the {0}. printing": "ordinal",
|
|
5
|
+
"removed in {0}. printing": "ordinal",
|
|
6
|
+
"{0} actions": "cardinal",
|
|
7
|
+
"{0} hours": "cardinal",
|
|
8
|
+
"{0} minutes": "cardinal",
|
|
9
|
+
"{0} rounds": "cardinal",
|
|
10
|
+
"{0} seduction actions": "cardinal",
|
|
11
|
+
", {0} of which are permanent": "cardinal",
|
|
12
|
+
"{0} centuries": "cardinal",
|
|
13
|
+
"{0} combat rounds": "cardinal",
|
|
14
|
+
"{0} days": "cardinal",
|
|
15
|
+
"{0} months": "cardinal",
|
|
16
|
+
"{0} mos.": "cardinal",
|
|
17
|
+
"{0} seconds": "cardinal",
|
|
18
|
+
"{0} weeks": "cardinal",
|
|
19
|
+
"{0} wks.": "cardinal",
|
|
20
|
+
"{0} years": "cardinal",
|
|
21
|
+
"{0} yrs.": "cardinal",
|
|
22
|
+
"{0} miles": "cardinal",
|
|
23
|
+
"{0} yards": "cardinal",
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Returns the plural rule type for the given key.
|
|
27
|
+
*/
|
|
28
|
+
export const getPluralRuleTypeForKey = (key) => pluralType[key];
|
package/lib/types/UI.d.ts
CHANGED
|
@@ -968,6 +968,8 @@ export type UI = {
|
|
|
968
968
|
"{0} KP": NonEmptyString;
|
|
969
969
|
"min. ": NonEmptyString;
|
|
970
970
|
"at least ": NonEmptyString;
|
|
971
|
+
"min. {0}": NonEmptyString;
|
|
972
|
+
"at least {0}": NonEmptyString;
|
|
971
973
|
"/{0}": NonEmptyString;
|
|
972
974
|
" per {0}": NonEmptyString;
|
|
973
975
|
", minimum of {0}": NonEmptyString;
|
|
@@ -981,6 +983,8 @@ export type UI = {
|
|
|
981
983
|
" for ": NonEmptyString;
|
|
982
984
|
" (no more than {0})": NonEmptyString;
|
|
983
985
|
" (max. {0})": NonEmptyString;
|
|
986
|
+
"no more than {0}": NonEmptyString;
|
|
987
|
+
"max. {0}": NonEmptyString;
|
|
984
988
|
"Immediate": NonEmptyString;
|
|
985
989
|
"Permanent": NonEmptyString;
|
|
986
990
|
"no more than ": NonEmptyString;
|
package/lib/validation/schema.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { assertExhaustive } from "@optolith/helpers/typeSafety";
|
|
1
2
|
import addFormatsModule from "ajv-formats";
|
|
2
3
|
import Ajv2019Module from "ajv/dist/2019.js";
|
|
3
4
|
import Ajv2020Module from "ajv/dist/2020.js";
|
|
4
5
|
import "../helpers/array.js";
|
|
5
6
|
import { isHiddenFileName, readDirectoryRec, readJsonFile } from "../helpers/io.js";
|
|
6
|
-
import { assertExhaustive } from "../helpers/typeSafety.js";
|
|
7
7
|
const Ajv2019 = Ajv2019Module.default;
|
|
8
8
|
const Ajv2020 = Ajv2020Module.default;
|
|
9
9
|
const addFormats = addFormatsModule.default;
|
|
@@ -15,7 +15,7 @@ const createSchemaValidator = (jsonSchemaSpec, validatorOptions = {}) => {
|
|
|
15
15
|
case "Draft_2020_12":
|
|
16
16
|
return new Ajv2020(validatorOptions);
|
|
17
17
|
default:
|
|
18
|
-
assertExhaustive(jsonSchemaSpec);
|
|
18
|
+
return assertExhaustive(jsonSchemaSpec);
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
21
|
const registerAllJsonSchemaDocuments = async (jsonSchemaDir, validator) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "optolith-database-schema",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.2",
|
|
4
4
|
"description": "Definitions and utilities for the flat-file database of Optolith, a character creation tool for the Pen and Paper RPG “The Dark Eye 5”, and its external integrations into other software.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"tde",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"./errors": "./lib/errors.js",
|
|
15
15
|
"./cache/*": "./lib/cache/*.js",
|
|
16
16
|
"./config/*": "./lib/config/*.js",
|
|
17
|
+
"./rendering/*": "./lib/rendering/*.js",
|
|
17
18
|
"./types/*": "./lib/types/*.js"
|
|
18
19
|
},
|
|
19
20
|
"scripts": {
|
|
@@ -28,6 +29,7 @@
|
|
|
28
29
|
"author": "Lukas Obermann",
|
|
29
30
|
"license": "MPL-2.0",
|
|
30
31
|
"dependencies": {
|
|
32
|
+
"@optolith/helpers": "^0.2.1",
|
|
31
33
|
"ajv": "^8.12.0",
|
|
32
34
|
"ajv-formats": "^2.1.1",
|
|
33
35
|
"yaml": "^2.3.3"
|
package/schema/UI.schema.json
CHANGED
|
@@ -2446,6 +2446,12 @@
|
|
|
2446
2446
|
"at least ": {
|
|
2447
2447
|
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2448
2448
|
},
|
|
2449
|
+
"min. {0}": {
|
|
2450
|
+
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2451
|
+
},
|
|
2452
|
+
"at least {0}": {
|
|
2453
|
+
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2454
|
+
},
|
|
2449
2455
|
"/{0}": {
|
|
2450
2456
|
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2451
2457
|
},
|
|
@@ -2485,6 +2491,12 @@
|
|
|
2485
2491
|
" (max. {0})": {
|
|
2486
2492
|
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2487
2493
|
},
|
|
2494
|
+
"no more than {0}": {
|
|
2495
|
+
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2496
|
+
},
|
|
2497
|
+
"max. {0}": {
|
|
2498
|
+
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2499
|
+
},
|
|
2488
2500
|
"Immediate": {
|
|
2489
2501
|
"$ref": "./_NonEmptyString.schema.json#/$defs/NonEmptyString"
|
|
2490
2502
|
},
|
|
@@ -3921,6 +3933,8 @@
|
|
|
3921
3933
|
"{0} KP",
|
|
3922
3934
|
"min. ",
|
|
3923
3935
|
"at least ",
|
|
3936
|
+
"min. {0}",
|
|
3937
|
+
"at least {0}",
|
|
3924
3938
|
"/{0}",
|
|
3925
3939
|
" per {0}",
|
|
3926
3940
|
", minimum of {0}",
|
|
@@ -3934,6 +3948,8 @@
|
|
|
3934
3948
|
" for ",
|
|
3935
3949
|
" (no more than {0})",
|
|
3936
3950
|
" (max. {0})",
|
|
3951
|
+
"no more than {0}",
|
|
3952
|
+
"max. {0}",
|
|
3937
3953
|
"Immediate",
|
|
3938
3954
|
"Permanent",
|
|
3939
3955
|
"no more than ",
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Extracts `null` and `undefined` from a type.
|
|
3
|
-
*/
|
|
4
|
-
export type Nullish<T = null | undefined> = T extends null | undefined ? T : never;
|
|
5
|
-
/**
|
|
6
|
-
* Checks if a value is `null` or `undefined`.
|
|
7
|
-
*/
|
|
8
|
-
export declare const isNullish: <T>(value: T) => value is Exclude<T, NonNullable<T>>;
|
|
9
|
-
/**
|
|
10
|
-
* Checks if a value is not `null` or `undefined`.
|
|
11
|
-
*/
|
|
12
|
-
export declare const isNotNullish: <T>(value: T) => value is NonNullable<T>;
|
|
13
|
-
/**
|
|
14
|
-
* Maps a value to another value if it is not `null` or `undefined`.
|
|
15
|
-
*/
|
|
16
|
-
export declare const mapNullable: <T, U>(value: T, map: (value: NonNullable<T>) => U) => U | Nullish<T>;
|
|
17
|
-
/**
|
|
18
|
-
* Maps a value to another value if it is not `null` or `undefined`, otherwise
|
|
19
|
-
* returns a default value.
|
|
20
|
-
*/
|
|
21
|
-
export declare const mapNullableDefault: <T, U>(value: T, map: (value: NonNullable<T>) => U, defaultValue: U) => U;
|
|
22
|
-
/**
|
|
23
|
-
* Returns an array, containing the value if it is not `null` or `undefined`.
|
|
24
|
-
*
|
|
25
|
-
* This can be useful in combination with the spread operator or
|
|
26
|
-
* `Array.prototype.flatMap`.
|
|
27
|
-
* @example
|
|
28
|
-
* nullableToArray(2) // [2]
|
|
29
|
-
* nullableToArray(undefined) // []
|
|
30
|
-
*
|
|
31
|
-
* [...nullableToArray(2)] // [2]
|
|
32
|
-
* [1, ...nullableToArray(2)] // [1, 2]
|
|
33
|
-
* [1, ...nullableToArray(undefined)] // [1]
|
|
34
|
-
*/
|
|
35
|
-
export declare const nullableToArray: <T>(value: T) => NonNullable<T>[];
|
package/lib/helpers/nullable.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Checks if a value is `null` or `undefined`.
|
|
3
|
-
*/
|
|
4
|
-
export const isNullish = (value) => value === null || value === undefined;
|
|
5
|
-
/**
|
|
6
|
-
* Checks if a value is not `null` or `undefined`.
|
|
7
|
-
*/
|
|
8
|
-
export const isNotNullish = (value) => !isNullish(value);
|
|
9
|
-
/**
|
|
10
|
-
* Maps a value to another value if it is not `null` or `undefined`.
|
|
11
|
-
*/
|
|
12
|
-
export const mapNullable = (value, map) => isNotNullish(value) ? map(value) : value;
|
|
13
|
-
/**
|
|
14
|
-
* Maps a value to another value if it is not `null` or `undefined`, otherwise
|
|
15
|
-
* returns a default value.
|
|
16
|
-
*/
|
|
17
|
-
export const mapNullableDefault = (value, map, defaultValue) => (isNotNullish(value) ? map(value) : defaultValue);
|
|
18
|
-
/**
|
|
19
|
-
* Returns an array, containing the value if it is not `null` or `undefined`.
|
|
20
|
-
*
|
|
21
|
-
* This can be useful in combination with the spread operator or
|
|
22
|
-
* `Array.prototype.flatMap`.
|
|
23
|
-
* @example
|
|
24
|
-
* nullableToArray(2) // [2]
|
|
25
|
-
* nullableToArray(undefined) // []
|
|
26
|
-
*
|
|
27
|
-
* [...nullableToArray(2)] // [2]
|
|
28
|
-
* [1, ...nullableToArray(2)] // [1, 2]
|
|
29
|
-
* [1, ...nullableToArray(undefined)] // [1]
|
|
30
|
-
*/
|
|
31
|
-
export const nullableToArray = (value) => isNotNullish(value) ? [value] : [];
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { describe, it } from "node:test";
|
|
3
|
-
import { isNotNullish, isNullish, mapNullable, mapNullableDefault, nullableToArray, } from "./nullable.js";
|
|
4
|
-
describe("isNullish", () => {
|
|
5
|
-
it("returns if a value is nullish", () => {
|
|
6
|
-
assert.equal(isNullish(null), true);
|
|
7
|
-
assert.equal(isNullish(undefined), true);
|
|
8
|
-
assert.equal(isNullish(false), false);
|
|
9
|
-
assert.equal(isNullish(0), false);
|
|
10
|
-
assert.equal(isNullish(""), false);
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
describe("isNotNullish", () => {
|
|
14
|
-
it("returns if a value is not nullish", () => {
|
|
15
|
-
assert.equal(isNotNullish(null), false);
|
|
16
|
-
assert.equal(isNotNullish(undefined), false);
|
|
17
|
-
assert.equal(isNotNullish(false), true);
|
|
18
|
-
assert.equal(isNotNullish(0), true);
|
|
19
|
-
assert.equal(isNotNullish(""), true);
|
|
20
|
-
});
|
|
21
|
-
});
|
|
22
|
-
describe("mapNullable", () => {
|
|
23
|
-
it("maps a value if it is not nullish", () => {
|
|
24
|
-
assert.equal(mapNullable(2, x => x * 2), 4);
|
|
25
|
-
});
|
|
26
|
-
it("returns the original value if it is nullish", () => {
|
|
27
|
-
assert.equal(mapNullable(undefined, x => x * 2), undefined);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
describe("mapNullableDefault", () => {
|
|
31
|
-
it("maps a value if it is not nullish", () => {
|
|
32
|
-
assert.equal(mapNullableDefault(2, x => x * 2, 0), 4);
|
|
33
|
-
});
|
|
34
|
-
it("returns a default if the value is nullish", () => {
|
|
35
|
-
assert.equal(mapNullableDefault(undefined, x => x * 2, 0), 0);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
38
|
-
describe("nullableToArray", () => {
|
|
39
|
-
it("wraps a non-nullish value into an array", () => {
|
|
40
|
-
assert.deepEqual(nullableToArray(2), [2]);
|
|
41
|
-
});
|
|
42
|
-
it("returns an empty array if the value is null or undefined", () => {
|
|
43
|
-
assert.deepEqual(nullableToArray(undefined), []);
|
|
44
|
-
assert.deepEqual(nullableToArray(null), []);
|
|
45
|
-
});
|
|
46
|
-
});
|
package/lib/helpers/object.d.ts
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Maps all own properties of an object to a new object. Returning `undefined`
|
|
3
|
-
* from the mapping function will omit the property from the result.
|
|
4
|
-
*/
|
|
5
|
-
export declare const mapObject: <T extends object, U>(object: T, map: (value: T[keyof T], key: keyof T) => U | undefined) => { [key in keyof T]: Exclude<U, undefined>; };
|
package/lib/helpers/object.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Maps all own properties of an object to a new object. Returning `undefined`
|
|
3
|
-
* from the mapping function will omit the property from the result.
|
|
4
|
-
*/
|
|
5
|
-
export const mapObject = (object, map) => {
|
|
6
|
-
const result = {};
|
|
7
|
-
for (const key in object) {
|
|
8
|
-
if (Object.hasOwn(object, key)) {
|
|
9
|
-
const newValue = map(object[key], key);
|
|
10
|
-
if (newValue !== undefined) {
|
|
11
|
-
result[key] = newValue;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
return result;
|
|
16
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { describe, it } from "node:test";
|
|
3
|
-
import { mapObject } from "./object.js";
|
|
4
|
-
describe("mapObject", () => {
|
|
5
|
-
it("maps all own properties of an object to a new object", () => {
|
|
6
|
-
const object = { a: 1, b: 2, c: 3 };
|
|
7
|
-
const result = mapObject(object, (value, key) => value + key);
|
|
8
|
-
assert.deepEqual(result, { a: "1a", b: "2b", c: "3c" });
|
|
9
|
-
});
|
|
10
|
-
it("omits properties for which the mapping function returns undefined", () => {
|
|
11
|
-
const object = { a: 1, b: 2, c: 3 };
|
|
12
|
-
const result = mapObject(object, (value, key) => (key === "b" ? undefined : value + key));
|
|
13
|
-
assert.deepEqual(result, { a: "1a", c: "3c" });
|
|
14
|
-
});
|
|
15
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This function is used to make sure that the `switch` is exhaustive. Place it
|
|
3
|
-
* in the `default` case of the `switch`.
|
|
4
|
-
* @param _x - The value that is used in the `switch`.
|
|
5
|
-
* @example
|
|
6
|
-
* const aorb = (x: "a" | "b") => {
|
|
7
|
-
* switch (x) {
|
|
8
|
-
* case "a": return 1
|
|
9
|
-
* case "b": return 2
|
|
10
|
-
* default: return assertExhaustive(x)
|
|
11
|
-
* }
|
|
12
|
-
* }
|
|
13
|
-
*/
|
|
14
|
-
export declare function assertExhaustive(_x: never): never;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This function is used to make sure that the `switch` is exhaustive. Place it
|
|
3
|
-
* in the `default` case of the `switch`.
|
|
4
|
-
* @param _x - The value that is used in the `switch`.
|
|
5
|
-
* @example
|
|
6
|
-
* const aorb = (x: "a" | "b") => {
|
|
7
|
-
* switch (x) {
|
|
8
|
-
* case "a": return 1
|
|
9
|
-
* case "b": return 2
|
|
10
|
-
* default: return assertExhaustive(x)
|
|
11
|
-
* }
|
|
12
|
-
* }
|
|
13
|
-
*/
|
|
14
|
-
export function assertExhaustive(_x) {
|
|
15
|
-
throw new Error("The switch is not exhaustive.");
|
|
16
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { describe, it } from "node:test";
|
|
3
|
-
import { assertExhaustive } from "./typeSafety.js";
|
|
4
|
-
describe("assertExhaustive", () => {
|
|
5
|
-
it("should throw an error with the message 'The switch is not exhaustive.'", () => {
|
|
6
|
-
assert.throws(
|
|
7
|
-
// @ts-expect-error The function should never receive a value.
|
|
8
|
-
() => assertExhaustive(""), err => err instanceof Error && err.message === "The switch is not exhaustive.");
|
|
9
|
-
});
|
|
10
|
-
});
|