@fgv/ts-json-base 5.0.0-2 → 5.0.0-21
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/.vscode/launch.json +16 -0
- package/.vscode/settings.json +33 -0
- package/config/api-extractor.json +343 -0
- package/config/rig.json +16 -0
- package/dist/tsdoc-metadata.json +1 -1
- package/lib/packlets/validators/validators.js +4 -4
- package/lib/test/unit/common.test.d.ts +2 -0
- package/lib/test/unit/converters.test.d.ts +2 -0
- package/lib/test/unit/data/file/bad/bad3.json +3 -0
- package/lib/test/unit/data/file/bad/thing1.json +4 -0
- package/lib/test/unit/data/file/bad/thing2.json +3 -0
- package/lib/test/unit/data/file/good/thing1.json +4 -0
- package/lib/test/unit/data/file/good/thing2.json +3 -0
- package/lib/test/unit/file.test.d.ts +2 -0
- package/lib/test/unit/jsonFsHelper.test.d.ts +2 -0
- package/lib/test/unit/validators.test.d.ts +2 -0
- package/package.json +10 -10
- package/src/index.ts +28 -0
- package/src/packlets/converters/converters.ts +174 -0
- package/src/packlets/converters/index.ts +23 -0
- package/src/packlets/json/common.ts +187 -0
- package/src/packlets/json/index.ts +23 -0
- package/src/packlets/json-file/file.ts +97 -0
- package/src/packlets/json-file/index.ts +25 -0
- package/src/packlets/json-file/jsonFsHelper.ts +262 -0
- package/src/packlets/json-file/jsonLike.ts +56 -0
- package/src/packlets/validators/index.ts +23 -0
- package/src/packlets/validators/validators.ts +155 -0
- package/CHANGELOG.md +0 -100
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/packlets/converters/converters.d.ts.map +0 -1
- package/lib/packlets/converters/converters.js.map +0 -1
- package/lib/packlets/converters/index.d.ts.map +0 -1
- package/lib/packlets/converters/index.js.map +0 -1
- package/lib/packlets/json/common.d.ts.map +0 -1
- package/lib/packlets/json/common.js.map +0 -1
- package/lib/packlets/json/index.d.ts.map +0 -1
- package/lib/packlets/json/index.js.map +0 -1
- package/lib/packlets/json-file/file.d.ts.map +0 -1
- package/lib/packlets/json-file/file.js.map +0 -1
- package/lib/packlets/json-file/index.d.ts.map +0 -1
- package/lib/packlets/json-file/index.js.map +0 -1
- package/lib/packlets/json-file/jsonFsHelper.d.ts.map +0 -1
- package/lib/packlets/json-file/jsonFsHelper.js.map +0 -1
- package/lib/packlets/json-file/jsonLike.d.ts.map +0 -1
- package/lib/packlets/json-file/jsonLike.js.map +0 -1
- package/lib/packlets/validators/index.d.ts.map +0 -1
- package/lib/packlets/validators/index.js.map +0 -1
- package/lib/packlets/validators/validators.d.ts.map +0 -1
- package/lib/packlets/validators/validators.js.map +0 -1
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2023 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Conversion, Converter, Result, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
+
import { JsonArray, JsonObject, JsonPrimitive, JsonValue, isJsonArray, isJsonObject } from '../json';
|
|
25
|
+
|
|
26
|
+
/* eslint-disable no-use-before-define, @typescript-eslint/no-use-before-define */
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Conversion context for JSON converters.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export interface IJsonConverterContext {
|
|
33
|
+
ignoreUndefinedProperties?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* An converter which converts a supplied `unknown` value to a valid {@link JsonPrimitive | JsonPrimitive}.
|
|
38
|
+
* @public
|
|
39
|
+
*/
|
|
40
|
+
export const jsonPrimitive: Converter<JsonPrimitive, IJsonConverterContext> = new Conversion.BaseConverter(
|
|
41
|
+
(
|
|
42
|
+
from: unknown,
|
|
43
|
+
__self: Converter<JsonPrimitive, IJsonConverterContext>,
|
|
44
|
+
ctx?: IJsonConverterContext
|
|
45
|
+
): Result<JsonPrimitive> => {
|
|
46
|
+
if (from === null) {
|
|
47
|
+
return succeed(null);
|
|
48
|
+
}
|
|
49
|
+
switch (typeof from) {
|
|
50
|
+
case 'boolean':
|
|
51
|
+
case 'string':
|
|
52
|
+
return succeed(from);
|
|
53
|
+
case 'number':
|
|
54
|
+
if (!Number.isNaN(from)) {
|
|
55
|
+
return succeed(from);
|
|
56
|
+
}
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
return fail(`"${String(from)}": not a valid JSON primitive.`);
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* An copying converter which converts a supplied `unknown` value into
|
|
65
|
+
* a valid {@link JsonObject | JsonObject}. Fails by default if any properties or array elements
|
|
66
|
+
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
67
|
+
* {@link Converters.IJsonConverterContext | context} at runtime.
|
|
68
|
+
*
|
|
69
|
+
* Guaranteed to return a new object.
|
|
70
|
+
* @public
|
|
71
|
+
*/
|
|
72
|
+
export const jsonObject: Converter<JsonObject, IJsonConverterContext> = new Conversion.BaseConverter(
|
|
73
|
+
(
|
|
74
|
+
from: unknown,
|
|
75
|
+
__self: Converter<JsonObject, IJsonConverterContext>,
|
|
76
|
+
ctx?: IJsonConverterContext
|
|
77
|
+
): Result<JsonObject> => {
|
|
78
|
+
if (!isJsonObject(from)) {
|
|
79
|
+
return fail('not a valid JSON object.');
|
|
80
|
+
}
|
|
81
|
+
const obj: JsonObject = {};
|
|
82
|
+
const errors: string[] = [];
|
|
83
|
+
for (const [name, value] of Object.entries(from)) {
|
|
84
|
+
if (value === undefined && ctx?.ignoreUndefinedProperties === true) {
|
|
85
|
+
// optionally ignore undefined values
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
jsonValue
|
|
89
|
+
.convert(value, ctx)
|
|
90
|
+
.onSuccess((v: JsonValue) => {
|
|
91
|
+
obj[name] = v;
|
|
92
|
+
return succeed(v);
|
|
93
|
+
})
|
|
94
|
+
.onFailure((m) => {
|
|
95
|
+
errors.push(`${name}: ${m}`);
|
|
96
|
+
return fail(m);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (errors.length > 0) {
|
|
100
|
+
return fail(`not a valid JSON object:\n${errors.join('\n')}`);
|
|
101
|
+
}
|
|
102
|
+
return succeed(obj);
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* An copying converter which converts a supplied `unknown` value to
|
|
108
|
+
* a valid {@link JsonArray | JsonArray}. Fails by default if any properties or array elements
|
|
109
|
+
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
110
|
+
* {@link Converters.IJsonConverterContext | context} at runtime.
|
|
111
|
+
*
|
|
112
|
+
* Guaranteed to return a new array.
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
115
|
+
export const jsonArray: Converter<JsonArray, IJsonConverterContext> = new Conversion.BaseConverter(
|
|
116
|
+
(
|
|
117
|
+
from: unknown,
|
|
118
|
+
__self: Converter<JsonArray, IJsonConverterContext>,
|
|
119
|
+
ctx?: IJsonConverterContext
|
|
120
|
+
): Result<JsonArray> => {
|
|
121
|
+
if (!isJsonArray(from)) {
|
|
122
|
+
return fail('not an array');
|
|
123
|
+
}
|
|
124
|
+
const arr: JsonValue[] = [];
|
|
125
|
+
const errors: string[] = [];
|
|
126
|
+
for (let i = 0; i < from.length; i++) {
|
|
127
|
+
const value = from[i];
|
|
128
|
+
if (value === undefined && ctx?.ignoreUndefinedProperties === true) {
|
|
129
|
+
// convert undefined to 'null' for parity with JSON.stringify
|
|
130
|
+
arr.push(null);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
jsonValue
|
|
134
|
+
.convert(value, ctx)
|
|
135
|
+
.onSuccess((v) => {
|
|
136
|
+
arr.push(v);
|
|
137
|
+
return succeed(v);
|
|
138
|
+
})
|
|
139
|
+
.onFailure((m) => {
|
|
140
|
+
errors.push(`${i}: ${m}`);
|
|
141
|
+
return fail(m);
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
if (errors.length > 0) {
|
|
145
|
+
return fail(`array contains non-json elements:\n${errors.join('\n')}`);
|
|
146
|
+
}
|
|
147
|
+
return succeed(arr);
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* An copying converter which converts a supplied `unknown` value to a
|
|
153
|
+
* valid {@link JsonValue | JsonValue}. Fails by default if any properties or array elements
|
|
154
|
+
* are `undefined` - this default behavior can be overridden by supplying an appropriate
|
|
155
|
+
* {@link Converters.IJsonConverterContext | context} at runtime.
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
export const jsonValue: Converter<JsonValue, IJsonConverterContext> = new Conversion.BaseConverter<
|
|
159
|
+
JsonValue,
|
|
160
|
+
IJsonConverterContext
|
|
161
|
+
>(
|
|
162
|
+
(
|
|
163
|
+
from: unknown,
|
|
164
|
+
__self: Converter<JsonValue, IJsonConverterContext>,
|
|
165
|
+
ctx?: IJsonConverterContext
|
|
166
|
+
): Result<JsonValue> => {
|
|
167
|
+
if (isJsonArray(from)) {
|
|
168
|
+
return jsonArray.convert(from, ctx);
|
|
169
|
+
} else if (isJsonObject(from)) {
|
|
170
|
+
return jsonObject.convert(from, ctx);
|
|
171
|
+
}
|
|
172
|
+
return jsonPrimitive.convert(from, ctx);
|
|
173
|
+
}
|
|
174
|
+
);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export * from './converters';
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Result, captureResult, fail, succeed } from '@fgv/ts-utils';
|
|
24
|
+
|
|
25
|
+
/* eslint-disable no-use-before-define */
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Primitive (terminal) values allowed in by JSON.
|
|
29
|
+
* @public
|
|
30
|
+
*/
|
|
31
|
+
// eslint-disable-next-line @rushstack/no-new-null
|
|
32
|
+
export type JsonPrimitive = boolean | number | string | null;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A {@link JsonObject | JsonObject} is a string-keyed object
|
|
36
|
+
* containing only valid {@link JsonValue | JSON values}.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
40
|
+
export interface JsonObject {
|
|
41
|
+
[key: string]: JsonValue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A {@link JsonValue | JsonValue} is one of: a {@link JsonPrimitive | JsonPrimitive},
|
|
46
|
+
* a {@link JsonObject | JsonObject} or an {@link JsonArray | JsonArray}.
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export type JsonValue = JsonPrimitive | JsonObject | JsonArray;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A {@link JsonArray | JsonArray} is an array containing only valid {@link JsonValue | JsonValues}.
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface, @typescript-eslint/naming-convention
|
|
56
|
+
export interface JsonArray extends Array<JsonValue> {}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Classes of {@link JsonValue | JsonValue}.
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export type JsonValueType = 'primitive' | 'object' | 'array';
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Test if an `unknown` is a {@link JsonValue | JsonValue}.
|
|
66
|
+
* @param from - The `unknown` to be tested
|
|
67
|
+
* @returns `true` if the supplied parameter is a valid {@link JsonPrimitive | JsonPrimitive},
|
|
68
|
+
* `false` otherwise.
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export function isJsonPrimitive(from: unknown): from is JsonPrimitive {
|
|
72
|
+
return typeof from === 'boolean' || typeof from === 'number' || typeof from === 'string' || from === null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Test if an `unknown` is potentially a {@link JsonObject | JsonObject}.
|
|
77
|
+
* @param from - The `unknown` to be tested.
|
|
78
|
+
* @returns `true` if the supplied parameter is a non-array, non-special object,
|
|
79
|
+
* `false` otherwise.
|
|
80
|
+
* @public
|
|
81
|
+
*/
|
|
82
|
+
export function isJsonObject(from: unknown): from is JsonObject {
|
|
83
|
+
return (
|
|
84
|
+
typeof from === 'object' &&
|
|
85
|
+
from !== null &&
|
|
86
|
+
!Array.isArray(from) &&
|
|
87
|
+
!(from instanceof RegExp) &&
|
|
88
|
+
!(from instanceof Date)
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Test if an `unknown` is potentially a {@link JsonArray | JsonArray}.
|
|
94
|
+
* @param from - The `unknown` to be tested.
|
|
95
|
+
* @returns `true` if the supplied parameter is an array object,
|
|
96
|
+
* `false` otherwise.
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
99
|
+
export function isJsonArray(from: unknown): from is JsonArray {
|
|
100
|
+
return typeof from === 'object' && Array.isArray(from);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Identifies whether some `unknown` value is a {@link JsonPrimitive | primitive},
|
|
105
|
+
* {@link JsonObject | object} or {@link JsonArray | array}. Fails for any value
|
|
106
|
+
* that cannot be converted to JSON (e.g. a function) _but_ this is a shallow test -
|
|
107
|
+
* it does not test the properties of an object or elements in an array.
|
|
108
|
+
* @param from - The `unknown` value to be tested
|
|
109
|
+
* @public
|
|
110
|
+
*/
|
|
111
|
+
export function classifyJsonValue(from: unknown): Result<JsonValueType> {
|
|
112
|
+
if (isJsonPrimitive(from)) {
|
|
113
|
+
return succeed('primitive');
|
|
114
|
+
} else if (isJsonObject(from)) {
|
|
115
|
+
return succeed('object');
|
|
116
|
+
} else if (isJsonArray(from)) {
|
|
117
|
+
return succeed('array');
|
|
118
|
+
}
|
|
119
|
+
return fail(`Invalid JSON: ${from}`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Picks a nested field from a supplied {@link JsonObject | JsonObject}.
|
|
124
|
+
* @param src - The {@link JsonObject | object} from which the field is to be picked.
|
|
125
|
+
* @param path - Dot-separated path of the member to be picked.
|
|
126
|
+
* @returns `Success` with the property if the path is valid, `Failure`
|
|
127
|
+
* with an error message otherwise.
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
export function pickJsonValue(src: JsonObject, path: string): Result<JsonValue> {
|
|
131
|
+
let result: JsonValue = src;
|
|
132
|
+
for (const part of path.split('.')) {
|
|
133
|
+
if (result && isJsonObject(result)) {
|
|
134
|
+
result = result[part];
|
|
135
|
+
if (result === undefined) {
|
|
136
|
+
return fail(`${path}: child '${part}' does not exist`);
|
|
137
|
+
}
|
|
138
|
+
} else {
|
|
139
|
+
return fail(`${path}: child '${part}' does not exist`);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return succeed(result);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Picks a nested {@link JsonObject | JsonObject} from a supplied
|
|
147
|
+
* {@link JsonObject | JsonObject}.
|
|
148
|
+
* @param src - The {@link JsonObject | object} from which the field is
|
|
149
|
+
* to be picked.
|
|
150
|
+
* @param path - Dot-separated path of the member to be picked.
|
|
151
|
+
* @returns `Success` with the property if the path is valid and the value
|
|
152
|
+
* is an object. Returns `Failure` with details if an error occurs.
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
155
|
+
export function pickJsonObject(src: JsonObject, path: string): Result<JsonObject> {
|
|
156
|
+
return pickJsonValue(src, path).onSuccess((v) => {
|
|
157
|
+
if (!isJsonObject(v)) {
|
|
158
|
+
return fail(`${path}: not an object`);
|
|
159
|
+
}
|
|
160
|
+
return succeed(v);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* "Sanitizes" an `unknown` by stringifying and then parsing it. Guarantees a
|
|
166
|
+
* valid {@link JsonValue | JsonValue} but is not idempotent and gives no guarantees
|
|
167
|
+
* about fidelity. Fails if the supplied value cannot be stringified as Json.
|
|
168
|
+
* @param from - The `unknown` to be sanitized.
|
|
169
|
+
* @returns `Success` with a {@link JsonValue | JsonValue} if conversion succeeds,
|
|
170
|
+
* `Failure` with details if an error occurs.
|
|
171
|
+
* @public
|
|
172
|
+
*/
|
|
173
|
+
export function sanitizeJson(from: unknown): Result<JsonValue> {
|
|
174
|
+
return captureResult(() => JSON.parse(JSON.stringify(from)));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Sanitizes some value using JSON stringification and parsing, returning an
|
|
179
|
+
* returning a matching strongly-typed value.
|
|
180
|
+
* @param from - The value to be sanitized.
|
|
181
|
+
* @returns `Success` with a {@link JsonObject | JsonObject} if conversion succeeds,
|
|
182
|
+
* `Failure` with details if an error occurs.
|
|
183
|
+
* @public
|
|
184
|
+
*/
|
|
185
|
+
export function sanitizeJsonObject<T>(from: T): Result<T> {
|
|
186
|
+
return captureResult(() => JSON.parse(JSON.stringify(from)));
|
|
187
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export * from './common';
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { Converter, Result } from '@fgv/ts-utils';
|
|
24
|
+
import { JsonValue } from '../json';
|
|
25
|
+
import {
|
|
26
|
+
DefaultJsonFsHelper,
|
|
27
|
+
IJsonFsDirectoryOptions,
|
|
28
|
+
IJsonFsDirectoryToMapOptions,
|
|
29
|
+
IReadDirectoryItem,
|
|
30
|
+
JsonFsHelper
|
|
31
|
+
} from './jsonFsHelper';
|
|
32
|
+
import { DefaultJsonLike } from './jsonLike';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* {@inheritdoc JsonFile.JsonFsHelper.readJsonFileSync}
|
|
36
|
+
* @public
|
|
37
|
+
*/
|
|
38
|
+
export function readJsonFileSync(srcPath: string): Result<JsonValue> {
|
|
39
|
+
return DefaultJsonFsHelper.readJsonFileSync(srcPath);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Read a JSON file and apply a supplied converter.
|
|
44
|
+
* @param srcPath - Path of the file to read.
|
|
45
|
+
* @param converter - `Converter` used to convert the file contents
|
|
46
|
+
* @returns `Success` with a result of type `<T>`, or `Failure`* with a message if an error occurs.
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export function convertJsonFileSync<T, TC = unknown>(
|
|
50
|
+
srcPath: string,
|
|
51
|
+
converter: Converter<T, TC>
|
|
52
|
+
): Result<T> {
|
|
53
|
+
return DefaultJsonFsHelper.convertJsonFileSync(srcPath, converter);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Reads all JSON files from a directory and apply a supplied converter.
|
|
58
|
+
* @param srcPath - The path of the folder to be read.
|
|
59
|
+
* @param options - {@link JsonFile.IJsonFsDirectoryOptions | Options} to control
|
|
60
|
+
* conversion and filtering
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export function convertJsonDirectorySync<T, TC = unknown>(
|
|
64
|
+
srcPath: string,
|
|
65
|
+
options: IJsonFsDirectoryOptions<T, TC>
|
|
66
|
+
): Result<IReadDirectoryItem<T>[]> {
|
|
67
|
+
return DefaultJsonFsHelper.convertJsonDirectorySync(srcPath, options);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Reads and converts all JSON files from a directory, returning a
|
|
72
|
+
* `Map<string, T>` indexed by file base name (i.e. minus the extension)
|
|
73
|
+
* with an optional name transformation applied if present.
|
|
74
|
+
* @param srcPath - The path of the folder to be read.
|
|
75
|
+
* @param options - {@link JsonFile.IJsonFsDirectoryToMapOptions | Options} to control conversion,
|
|
76
|
+
* filtering and naming.
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
export function convertJsonDirectoryToMapSync<T, TC = unknown>(
|
|
80
|
+
srcPath: string,
|
|
81
|
+
options: IJsonFsDirectoryToMapOptions<T, TC>
|
|
82
|
+
): Result<Map<string, T>> {
|
|
83
|
+
return DefaultJsonFsHelper.convertJsonDirectoryToMapSync(srcPath, options);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const CompatJsonFsHelper: JsonFsHelper = new JsonFsHelper({
|
|
87
|
+
json: DefaultJsonLike,
|
|
88
|
+
allowUndefinedWrite: true // for compatibility
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* {@inheritDoc JsonFile.JsonFsHelper.writeJsonFileSync}
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
export function writeJsonFileSync(srcPath: string, value: JsonValue): Result<boolean> {
|
|
96
|
+
return CompatJsonFsHelper.writeJsonFileSync(srcPath, value);
|
|
97
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export * from './file';
|
|
24
|
+
export * from './jsonFsHelper';
|
|
25
|
+
export * from './jsonLike';
|