Skip to content

Commit 288c96d

Browse files
committed
[hygiene] Shrink jsonString
1 parent 3213846 commit 288c96d

File tree

10 files changed

+19
-26
lines changed

10 files changed

+19
-26
lines changed

src/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import {TablesSchema, ValuesSchema} from './types/store.d';
44
import {dirname, resolve} from 'path';
5-
import {isArray, jsonParse} from './common/other';
65
import {readFileSync, writeFileSync} from 'fs';
76
import {UTF8} from './common/strings';
87
import {arrayForEach} from './common/array';
98
import {createStore} from './tinybase';
109
import {createTools} from './tools';
1110
import {fileURLToPath} from 'url';
11+
import {isArray} from './common/other';
12+
import {jsonParse} from './common/json';
1213
import {objMap} from './common/obj';
1314

1415
const FILE_ERROR = 'provide a valid schemaFile, storeName, and outputDir';

src/common/json.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {isInstanceOf} from './other';
2+
import {object} from './obj';
3+
4+
export const jsonString = (obj: unknown): string =>
5+
JSON.stringify(obj, (_key, value) =>
6+
isInstanceOf(value, Map) ? object.fromEntries([...value]) : value,
7+
);
8+
9+
export const jsonParse = JSON.parse;

src/common/obj.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {Id} from '../types/common.d';
55
export type IdObj<Value> = {[id: string]: Value};
66
export type IdObj2<Value> = IdObj<IdObj<Value>>;
77

8-
const object = Object;
8+
export const object = Object;
99

1010
export const objIds = object.keys;
1111
export const objFrozen = object.isFrozen;

src/common/other.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
11
import {BOOLEAN, FUNCTION, STRING, getTypeOf} from './strings';
2-
import {arrayReduce} from './array';
32

43
const promise = Promise;
54

65
export const DEBUG = (globalThis as any).DEBUG ?? true;
76

8-
export const jsonString = (obj: unknown): string =>
9-
JSON.stringify(obj, (_key, value) =>
10-
isInstanceOf(value, Map)
11-
? arrayReduce(
12-
[...value],
13-
(obj: {[key: string]: unknown}, [key, value]) => {
14-
obj[key] = value;
15-
return obj;
16-
},
17-
{},
18-
)
19-
: value,
20-
);
21-
22-
export const jsonParse = JSON.parse;
23-
247
export const mathMax = Math.max;
258

269
export const mathMin = Math.min;

src/persisters/persister-browser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
createLocalPersister as createLocalPersisterDecl,
55
createSessionPersister as createSessionPersisterDecl,
66
} from '../types/persisters/persister-browser';
7-
import {jsonParse, jsonString} from '../common/other';
7+
import {jsonParse, jsonString} from '../common/json';
88
import {createCustomPersister} from '../persisters';
99

1010
type StoreListener = (event: StorageEvent) => void;

src/persisters/persister-file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {FSWatcher, watch} from 'fs';
22
import {Persister, PersisterListener} from '../types/persisters';
33
import {Store, Tables, Values} from '../types/store';
4-
import {jsonParse, jsonString} from '../common/other';
4+
import {jsonParse, jsonString} from '../common/json';
55
import {readFile, writeFile} from 'fs/promises';
66
import {UTF8} from '../common/strings';
77
import {createCustomPersister} from '../persisters';

src/persisters/persister-remote.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import {Persister, PersisterListener} from '../types/persisters';
22
import {Store, Tables, Values} from '../types/store';
3-
import {isUndefined, jsonParse, jsonString} from '../common/other';
3+
import {jsonParse, jsonString} from '../common/json';
44
import {createCustomPersister} from '../persisters';
55
import {createRemotePersister as createRemotePersisterDecl} from '../types/persisters/persister-remote';
6+
import {isUndefined} from '../common/other';
67

78
const getETag = (response: Response) => response.headers.get('ETag');
89

src/persisters/sqlite/json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Cmd, getCommandFunctions} from './commands';
22
import {DEFAULT_ROW_ID_COLUMN_NAME, SINGLE_ROW_ID} from './common';
33
import {Persister, PersisterListener} from '../../types/persisters';
44
import {Store, Tables, Values} from '../../types/store';
5-
import {jsonParse, jsonString} from '../../common/other';
5+
import {jsonParse, jsonString} from '../../common/json';
66
import {DefaultedJsonConfig} from './config';
77
import {createCustomPersister} from '../../persisters';
88

src/store.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ import {
5757
isFunction,
5858
isTypeStringOrBoolean,
5959
isUndefined,
60-
jsonParse,
61-
jsonString,
6260
} from './common/other';
6361
import {
6462
ExtraArgsGetter,
@@ -126,6 +124,7 @@ import {
126124
collSize4,
127125
} from './common/coll';
128126
import {getCellOrValueType, setOrDelCell, setOrDelValue} from './common/cell';
127+
import {jsonParse, jsonString} from './common/json';
129128
import {defaultSorter} from './common';
130129

131130
type TablesSchemaMap = IdMap2<CellSchema>;

src/tools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {objFreeze, objIsEmpty} from './common/obj';
1313
import {collForEach} from './common/coll';
1414
import {getCreateFunction} from './common/definable';
1515
import {getStoreApi as getStoreApiImpl} from './tools/api/api';
16-
import {jsonParse} from './common/other';
16+
import {jsonParse} from './common/json';
1717

1818
type CellMeta = [string, IdMap<number>, [number, Cell?], number];
1919

0 commit comments

Comments
 (0)