fat-json 0.0.4 → 0.0.6

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Fat Json
2
2
 
3
- Stringify and parse non-standard JSON data types: _reviver_ and _replacer_ functions for the built-in `JSON` object
3
+ Stringify and parse non-standard JSON [data types](#from-wikipedia): _reviver_ and _replacer_ functions for the built-in `JSON` object
4
4
 
5
5
  ## Install
6
6
 
@@ -12,7 +12,11 @@ npm i fat-json
12
12
 
13
13
  Transforming _standard data types_ using the built-in `JSON` object is simple, but the JavaScript language now features other types like `BigInt` which don't serialise/cannot be coerced to another type without potential data loss
14
14
 
15
- Serialising and de-serialising with `JSON.stringify()` and `JSON.parse()` provides _some_ opportunity to safely transform data from one type to another with _replacer_ and _reviver_ functions, but while it's not hard to argue with the simplicity of transforming from a `BigInt` to a `String` ...
15
+ Serialising and de-serialising with `JSON.stringify()` and `JSON.parse()` provides _some_ opportunity to safely transform data from one type to another with _replacer_ and _reviver_ functions
16
+
17
+ #### A typical replacer function for `BigInt`
18
+
19
+ We can test the `value` and transform based on its type
16
20
 
17
21
  ```javascript
18
22
  function replacer (key, value) {
@@ -22,7 +26,11 @@ function replacer (key, value) {
22
26
  }
23
27
  ```
24
28
 
25
- ... it's harder to see how to transform back from a `String` to a `BigInt` without additional information
29
+ It's hard to argue with the simplicity of transforming from a `BigInt` to a `String` but it's harder to see how we should transform back from a `String` to a `BigInt` without some _additional information_
30
+
31
+ #### A typical reviver function
32
+
33
+ Should we transform every `String`?
26
34
 
27
35
  ```javascript
28
36
  function reviver (key, value) {
@@ -32,9 +40,11 @@ function reviver (key, value) {
32
40
  }
33
41
  ```
34
42
 
35
- Should we use a catch block? Format the _key_ or _value_ in such away that we can test them, and then decide what to do?
43
+ We could use a catch block. Or else format the _key_ or _value_ such that we can test one or both of them, and then decide what to do
44
+
45
+ ## Fat Json's third argument
36
46
 
37
- _Fat Json_ will supply a _[JSONPath](https://en.wikipedia.org/wiki/JSONPath)_ as the third argument to your _replacer_ or _reviver_ function. If you know the structure of your data in advance you can serialise and de-serialise safely and in one step
47
+ _Fat Json_ supplies a _[JSONPath](https://en.wikipedia.org/wiki/JSONPath)_ as the third argument to your _replacer_ or _reviver_ function so you can decide _whether to transform a value by its position in the JSON document_, not just by its `key` or `value`. If you know the structure of your data in advance you can serialise and de-serialise safely and in one step
38
48
 
39
49
  ```javascript
40
50
  import {
package/index.d.mts ADDED
@@ -0,0 +1,32 @@
1
+ declare global {
2
+ namespace FatJsonTypes {
3
+ export type ArrayLiteralType = unknown[] | never[]
4
+ export type ObjectLiteralType = Record<PropertyKey, unknown> | Record<PropertyKey, never>
5
+ export type ValueType = string | number | boolean | null | bigint | ArrayLiteralType | ObjectLiteralType | Buffer | undefined | object
6
+
7
+ export namespace PathTypes {
8
+ export interface ArrayPathDescriptor {
9
+ key: PropertyKey
10
+ value: ValueType
11
+ path: string
12
+ context: ArrayLiteralType
13
+ }
14
+
15
+ export interface ObjectPathDescriptor {
16
+ key: PropertyKey
17
+ value: ValueType
18
+ path: string
19
+ context: ObjectLiteralType
20
+ }
21
+
22
+ export interface PathDescriptor {
23
+ key: PropertyKey
24
+ value: ValueType
25
+ path: string
26
+ context: ArrayLiteralType | ObjectLiteralType
27
+ }
28
+ }
29
+ }
30
+ }
31
+
32
+ export {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fat-json",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "keywords": [
5
5
  "JSON",
6
6
  "stringify",
@@ -12,7 +12,7 @@
12
12
  ],
13
13
  "main": "./src/index.mjs",
14
14
  "type": "module",
15
- "types": "./src/index.d.mts",
15
+ "types": "./index.d.mts",
16
16
  "author": {
17
17
  "name": "Jonathan Perry for Sequence Media Limited",
18
18
  "email": "sequencemedia@sequencemedia.net",
@@ -30,11 +30,11 @@
30
30
  "test": "mocha test --recursive"
31
31
  },
32
32
  "devDependencies": {
33
- "@sequencemedia/eslint-config-standard": "^0.2.82",
34
- "@sequencemedia/eslint-config-typescript": "^0.1.141",
33
+ "@sequencemedia/eslint-config-standard": "^0.2.83",
34
+ "@sequencemedia/eslint-config-typescript": "^0.1.142",
35
35
  "@types/chai": "^5.2.2",
36
36
  "@types/mocha": "^10.0.10",
37
- "@types/node": "^24.5.0",
37
+ "@types/node": "^24.5.1",
38
38
  "@types/sinon": "^17.0.4",
39
39
  "chai": "^6.0.1",
40
40
  "eslint": "^9.35.0",
@@ -47,6 +47,10 @@
47
47
  "imports": {
48
48
  "#common": "./src/common/index.mjs",
49
49
  "#fat-json": "./src/index.mjs",
50
- "#transformers": "./src/transformers/index.mjs"
50
+ "#transformers": "./src/transformers/index.mjs",
51
+ "#types": "./types/index.d.mts"
52
+ },
53
+ "exports": {
54
+ "./transformers": "./src/transformers/index.mjs"
51
55
  }
52
56
  }
@@ -1,4 +1,4 @@
1
- type ValueType = FatJsonTypes.ValueType
1
+ export type ValueType = FatJsonTypes.ValueType
2
2
 
3
3
  declare const BIG_INT: RegExp
4
4
 
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @typedef {FatJsonTypes.ValueType} ValueType
2
+ * @typedef {import('#types').ValueType} ValueType
3
3
  */
4
4
 
5
5
  export const BIG_INT = /^(\d+)n$/
package/src/index.d.mts CHANGED
@@ -1,9 +1,11 @@
1
- declare global {
2
- namespace FatJsonTypes {
3
- export type ArrayLiteralType = unknown[] | never[]
4
- export type ObjectLiteralType = Record<PropertyKey, unknown> | Record<PropertyKey, never>
5
- export type ValueType = string | number | boolean | null | bigint | ArrayLiteralType | ObjectLiteralType | Buffer | undefined | object
6
- }
7
- }
8
-
9
- export {}
1
+ export type ArrayLiteralType = FatJsonTypes.ArrayLiteralType
2
+ export type ObjectLiteralType = FatJsonTypes.ObjectLiteralType
3
+ export type ValueType = FatJsonTypes.ValueType
4
+
5
+ export function useReplacerWithType (replacer: (key: PropertyKey, value: ValueType, type?: string) => ValueType): (key: PropertyKey, value: ValueType) => ValueType
6
+
7
+ export function useReviverWithType (reviver: (key: PropertyKey, value: ValueType, type?: string) => ValueType): (key: PropertyKey, value: ValueType) => ValueType
8
+
9
+ export function useReplacerWithPath (replacer: (key: PropertyKey, value: ValueType, path?: string) => ValueType) : (key: PropertyKey, value: ValueType) => ValueType
10
+
11
+ export function useReviverWithPath (reviver: (key: PropertyKey, value: ValueType, path?: string) => ValueType): (key: PropertyKey, value: ValueType) => ValueType
package/src/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  /**
2
- * @typedef {FatJsonTypes.ValueType} ValueType
2
+ * @typedef {import('#types').ValueType} ValueType
3
3
  */
4
+
4
5
  import getPath from './replacer/index.mjs'
5
6
  import genPath from './reviver/index.mjs'
6
7
 
@@ -1,5 +1,5 @@
1
- type ArrayLiteralType = FatJsonTypes.ArrayLiteralType
2
- type ObjectLiteralType = FatJsonTypes.ObjectLiteralType
3
- type ValueType = FatJsonTypes.ValueType
1
+ export type ArrayLiteralType = FatJsonTypes.ArrayLiteralType
2
+ export type ObjectLiteralType = FatJsonTypes.ObjectLiteralType
3
+ export type ValueType = FatJsonTypes.ValueType
4
4
 
5
5
  export default function getPath (key: PropertyKey, value: ValueType, context: ArrayLiteralType | ObjectLiteralType): string
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @typedef {import('#types').ArrayLiteralType} ArrayLiteralType
3
+ * @typedef {import('#types').ObjectLiteralType} ObjectLiteralType
4
+ * @typedef {import('#types').ValueType} ValueType
5
+ */
6
+
1
7
  import {
2
8
  formatNumber,
3
9
  formatString,
@@ -5,12 +11,15 @@ import {
5
11
  isObject
6
12
  } from '#common'
7
13
 
14
+ /**
15
+ * @type {WeakMap<WeakKey, ArrayLiteralType | ObjectLiteralType>}
16
+ */
8
17
  const MAP = new WeakMap()
9
18
 
10
19
  /**
11
20
  * @param {PropertyKey} key
12
- * @param {unknown} value
13
- * @param {any} context
21
+ * @param {ValueType} value
22
+ * @param {ArrayLiteralType | ObjectLiteralType} context
14
23
  * @returns {string}
15
24
  */
16
25
  export default function getPath (key, value, context) {
@@ -1,5 +1,11 @@
1
- type ArrayLiteralType = FatJsonTypes.ArrayLiteralType
2
- type ObjectLiteralType = FatJsonTypes.ObjectLiteralType
3
- type ValueType = FatJsonTypes.ValueType
1
+ export type ArrayLiteralType = FatJsonTypes.ArrayLiteralType
2
+ export type ObjectLiteralType = FatJsonTypes.ObjectLiteralType
3
+ export type ValueType = FatJsonTypes.ValueType
4
4
 
5
- export default function genPath (key: PropertyKey, value: ValueType, context: ArrayLiteralType | ObjectLiteralType): IterableIterator<{ key: PropertyKey; value: ValueType; path: string; context: ArrayLiteralType | ObjectLiteralType }>
5
+ export namespace PathTypes {
6
+ export type ArrayPathDescriptor = FatJsonTypes.PathTypes.ArrayPathDescriptor
7
+ export type ObjectPathDescriptor = FatJsonTypes.PathTypes.ObjectPathDescriptor
8
+ export type PathDescriptor = FatJsonTypes.PathTypes.PathDescriptor
9
+ }
10
+
11
+ export default function genPath (key: PropertyKey, value: ValueType, context: ArrayLiteralType | ObjectLiteralType): IterableIterator<PathTypes.PathDescriptor>
@@ -1,8 +1,10 @@
1
1
  /**
2
- * @typedef {FatJsonTypes.ArrayLiteralType} ArrayLiteralType
3
- * @typedef {FatJsonTypes.ObjectLiteralType} ObjectLiteralType
4
- * @typedef {FatJsonTypes.ValueType} ValueType
5
- * @typedef {FatJsonTypes.WeakMapType} WeakMapType
2
+ * @typedef {import('#types').ArrayLiteralType} ArrayLiteralType
3
+ * @typedef {import('#types').ObjectLiteralType} ObjectLiteralType
4
+ * @typedef {import('#types').ValueType} ValueType
5
+ * @typedef {import('#types').PathTypes.ArrayPathDescriptor} PathTypes.ArrayPathDescriptor
6
+ * @typedef {import('#types').PathTypes.ObjectPathDescriptor} PathTypes.ObjectPathDescriptor
7
+ * @typedef {import('#types').PathTypes.PathDescriptor} PathTypes.PathDescriptor
6
8
  */
7
9
 
8
10
  import {
@@ -13,9 +15,9 @@ import {
13
15
  } from '#common'
14
16
 
15
17
  /**
16
- * @param {any[]} context
18
+ * @param {ArrayLiteralType} context
17
19
  * @param {string} contextPath
18
- * @returns {IterableIterator<{ key: PropertyKey; value: unknown; path: string; context: any }>}
20
+ * @returns {IterableIterator<PathTypes.ArrayPathDescriptor>}
19
21
  */
20
22
  function * genPathForArray (context, contextPath) {
21
23
  for (const [key, value] of context.entries()) {
@@ -37,9 +39,9 @@ function * genPathForArray (context, contextPath) {
37
39
  }
38
40
 
39
41
  /**
40
- * @param {any} context
42
+ * @param {ObjectLiteralType} context
41
43
  * @param {string} contextPath
42
- * @returns {IterableIterator<{ key: PropertyKey; value: unknown; path: string; context: any }>}
44
+ * @returns {IterableIterator<PathTypes.ObjectPathDescriptor>}
43
45
  */
44
46
  function * genPathForObject (context, contextPath) {
45
47
  for (const [key, value] of Object.entries(context)) {
@@ -62,9 +64,9 @@ function * genPathForObject (context, contextPath) {
62
64
 
63
65
  /**
64
66
  * @param {PropertyKey} key
65
- * @param {unknown} value
66
- * @param {any | any[]} context
67
- * @returns {IterableIterator<{ key: PropertyKey; value: unknown; path: string; context: any }>}
67
+ * @param {ValueType} value
68
+ * @param {ArrayLiteralType | ObjectLiteralType} context
69
+ * @returns {IterableIterator<PathTypes.PathDescriptor>}
68
70
  */
69
71
  export default function * genPath (key, value, context) {
70
72
  const valuePath = key
@@ -0,0 +1,9 @@
1
+ export type ArrayLiteralType = FatJsonTypes.ArrayLiteralType
2
+ export type ObjectLiteralType = FatJsonTypes.ObjectLiteralType
3
+ export type ValueType = FatJsonTypes.ValueType
4
+
5
+ export namespace PathTypes {
6
+ export type ArrayPathDescriptor = FatJsonTypes.PathTypes.ArrayPathDescriptor
7
+ export type ObjectPathDescriptor = FatJsonTypes.PathTypes.ObjectPathDescriptor
8
+ export type PathDescriptor = FatJsonTypes.PathTypes.PathDescriptor
9
+ }