|
1 | 1 | import { ensure } from './ensure'; |
2 | | -import { equal, isRecord, significantFieldsOf, stringify } from './objects'; |
| 2 | +import { equal, significantFieldsOf, stringify, toJSON } from './objects'; |
3 | 3 | import { isDefined } from './predicates'; |
4 | | -import { JSONObject, JSONValue, Serialisable } from './types'; |
| 4 | +import { JSONValue, Serialisable } from './types'; |
5 | 5 |
|
6 | 6 | /** |
7 | 7 | * @desc The {@link TinyTypeOf} can be used to define simple |
@@ -137,68 +137,3 @@ export abstract class TinyType implements Serialisable { |
137 | 137 | }, {}) as JSONValue; |
138 | 138 | } |
139 | 139 | } |
140 | | - |
141 | | -function toJSON(value: any): JSONValue | undefined { |
142 | | - switch (true) { |
143 | | - case value && !! value.toJSON: |
144 | | - return value.toJSON(); |
145 | | - case value && Array.isArray(value): |
146 | | - return value.map(v => { |
147 | | - return v === undefined |
148 | | - ? null |
149 | | - : toJSON(v) as JSONValue; |
150 | | - }); |
151 | | - case value && value instanceof Map: |
152 | | - return mapToJSON(value); |
153 | | - case value && value instanceof Set: |
154 | | - return toJSON(Array.from(value)); |
155 | | - case value && isRecord(value): |
156 | | - return recordToJSON(value); |
157 | | - case value && value instanceof Error: |
158 | | - return errorToJSON(value); |
159 | | - case isSerialisablePrimitive(value): |
160 | | - return value; |
161 | | - default: |
162 | | - return JSON.stringify(value); |
163 | | - } |
164 | | -} |
165 | | - |
166 | | -function mapToJSON(map: Map<any, any>): JSONObject { |
167 | | - const serialised = Array.from(map, ([key, value]) => [ toJSON(key), toJSON(value) ]); |
168 | | - |
169 | | - return Object.fromEntries(serialised); |
170 | | -} |
171 | | - |
172 | | -function recordToJSON(value: Record<any, any>): JSONObject { |
173 | | - const serialised = Object.entries(value) |
174 | | - .map(([ k, v ]) => [ toJSON(k), toJSON(v) ]); |
175 | | - |
176 | | - return Object.fromEntries(serialised); |
177 | | -} |
178 | | - |
179 | | -function errorToJSON(value: Error): JSONObject { |
180 | | - return Object.getOwnPropertyNames(value) |
181 | | - .reduce((serialised, key) => { |
182 | | - serialised[key] = toJSON(value[key]) |
183 | | - return serialised; |
184 | | - }, { }) as JSONObject; |
185 | | -} |
186 | | - |
187 | | -function isSerialisableNumber(value: unknown): value is number { |
188 | | - return typeof value === 'number' |
189 | | - && ! Number.isNaN(value) |
190 | | - && value !== Number.NEGATIVE_INFINITY |
191 | | - && value !== Number.POSITIVE_INFINITY; |
192 | | -} |
193 | | - |
194 | | -function isSerialisablePrimitive(value: unknown): value is string | boolean | number | null | undefined { |
195 | | - if (['string', 'boolean'].includes(typeof value)) { |
196 | | - return true; |
197 | | - } |
198 | | - |
199 | | - if (value === null || value === undefined) { |
200 | | - return true; |
201 | | - } |
202 | | - |
203 | | - return isSerialisableNumber(value); |
204 | | -} |
0 commit comments