@brillout/json-serializer 0.5.19-commit-8a9605c → 0.5.20
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/examples.js +44 -0
- package/package.json +2 -2
- package/readme.md +15 -16
package/examples.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run all examples in the examples/ directory
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { readdir } from 'fs/promises'
|
|
6
|
+
import { fileURLToPath } from 'url'
|
|
7
|
+
import { dirname, join } from 'path'
|
|
8
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
9
|
+
const __dirname = dirname(__filename)
|
|
10
|
+
|
|
11
|
+
runAllExamples()
|
|
12
|
+
|
|
13
|
+
async function runAllExamples() {
|
|
14
|
+
console.log('🎯 Running all json-serializer examples...\n')
|
|
15
|
+
|
|
16
|
+
// Read all .js files from examples directory
|
|
17
|
+
const files = await readdir(join(__dirname, 'examples'))
|
|
18
|
+
const examples = files.filter((file) => file.endsWith('.js')).sort()
|
|
19
|
+
|
|
20
|
+
let successCount = 0
|
|
21
|
+
let failureCount = 0
|
|
22
|
+
|
|
23
|
+
for (const example of examples) {
|
|
24
|
+
try {
|
|
25
|
+
console.log(`🚀 Running ${example}...`)
|
|
26
|
+
await import(`./examples/${example}`)
|
|
27
|
+
console.log(`✅ ${example} completed successfully\n`)
|
|
28
|
+
successCount++
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.log(`❌ ${example} failed: ${error.message}\n`)
|
|
31
|
+
failureCount++
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
console.log('='.repeat(50))
|
|
36
|
+
console.log(`✅ Successful: ${successCount}`)
|
|
37
|
+
if (failureCount > 0) {
|
|
38
|
+
console.log(`❌ Failed: ${failureCount}`)
|
|
39
|
+
console.log(`📁 Total: ${examples.length}`)
|
|
40
|
+
process.exit(1)
|
|
41
|
+
} else {
|
|
42
|
+
console.log('🎉 All examples completed successfully!')
|
|
43
|
+
}
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@brillout/json-serializer",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.20",
|
|
4
4
|
"description": "Same as JSON but with added support for `Date`, `undefined`, `Map`, `Set`, and more.",
|
|
5
5
|
"main": "./index.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"========= Dev": "",
|
|
46
|
-
"dev": "
|
|
46
|
+
"dev": "tsc --watch",
|
|
47
47
|
"========= Build": "",
|
|
48
48
|
"build": "rm -rf dist/ && tsc",
|
|
49
49
|
"========= Test": "",
|
package/readme.md
CHANGED
|
@@ -91,10 +91,10 @@ JSON is a good serializer for JavaScript values but
|
|
|
91
91
|
is lacking some JavaScript types such as `Date`:
|
|
92
92
|
|
|
93
93
|
~~~js
|
|
94
|
-
|
|
94
|
+
import assert from 'assert'
|
|
95
95
|
|
|
96
96
|
let obj = {
|
|
97
|
-
time: new Date('2042-01-01')
|
|
97
|
+
time: new Date('2042-01-01'),
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
// JSON converts dates to strings
|
|
@@ -107,12 +107,12 @@ assert(obj.time === '2042-01-01T00:00:00.000Z')
|
|
|
107
107
|
Whereas with `@brillout/json-serializer`:
|
|
108
108
|
|
|
109
109
|
~~~js
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
110
|
+
import { parse } from '@brillout/json-serializer/parse'
|
|
111
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
112
|
+
import assert from 'assert'
|
|
113
113
|
|
|
114
114
|
let obj = {
|
|
115
|
-
time: new Date('2042-01-01')
|
|
115
|
+
time: new Date('2042-01-01'),
|
|
116
116
|
}
|
|
117
117
|
|
|
118
118
|
// `@brillout/json-serializer` preserves Date
|
|
@@ -137,12 +137,12 @@ assert(obj.time.getTime() === new Date('2042-01-01').getTime())
|
|
|
137
137
|
|
|
138
138
|
~~~js
|
|
139
139
|
// npm install @brillout/json-serializer
|
|
140
|
-
|
|
141
|
-
|
|
140
|
+
import { parse } from '@brillout/json-serializer/parse'
|
|
141
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
142
142
|
|
|
143
143
|
const obj = {
|
|
144
144
|
hello: 'from the future',
|
|
145
|
-
time: new Date('2042-01-01')
|
|
145
|
+
time: new Date('2042-01-01'),
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
// Serialize with @brillout/json-serializer
|
|
@@ -161,17 +161,16 @@ Example exposing all differences between JSON and `@brillout/json-serializer`.
|
|
|
161
161
|
~~~js
|
|
162
162
|
// /examples/json-serializer.js
|
|
163
163
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const { stringify } = require('@brillout/json-serializer/stringify')
|
|
164
|
+
import { parse } from '@brillout/json-serializer/parse'
|
|
165
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
166
|
+
import assert from 'assert'
|
|
168
167
|
|
|
169
168
|
const obj = {
|
|
170
169
|
date: new Date(),
|
|
171
170
|
undefined: undefined,
|
|
172
171
|
NaN: NaN,
|
|
173
172
|
Infinity: Infinity,
|
|
174
|
-
regexp: /^\d+$/g
|
|
173
|
+
regexp: /^\d+$/g,
|
|
175
174
|
}
|
|
176
175
|
|
|
177
176
|
// All of `obj` can be serialized with @brillout/json-serializer
|
|
@@ -217,7 +216,7 @@ Let's see how `@brillout/json-serializer` serializes an object:
|
|
|
217
216
|
~~~js
|
|
218
217
|
// /examples/inspect.js
|
|
219
218
|
|
|
220
|
-
|
|
219
|
+
import { stringify } from '@brillout/json-serializer/stringify'
|
|
221
220
|
|
|
222
221
|
const obj = {
|
|
223
222
|
date: new Date(),
|
|
@@ -225,7 +224,7 @@ const obj = {
|
|
|
225
224
|
collision: '!undefined',
|
|
226
225
|
NaN: NaN,
|
|
227
226
|
Infinity: Infinity,
|
|
228
|
-
regexp: /^\d+$/g
|
|
227
|
+
regexp: /^\d+$/g,
|
|
229
228
|
}
|
|
230
229
|
|
|
231
230
|
console.log(stringify(obj, undefined, 2))
|