adapt-authoring-core 2.0.0 → 2.1.0
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/index.js +1 -1
- package/lib/Utils.js +1 -0
- package/lib/utils/stringifyValues.js +18 -0
- package/package.json +1 -1
- package/tests/utils-stringifyValues.spec.js +67 -0
- package/.github/workflows/test.yml +0 -13
package/index.js
CHANGED
|
@@ -2,4 +2,4 @@ export { default as AbstractModule } from './lib/AbstractModule.js'
|
|
|
2
2
|
export { default as App } from './lib/App.js'
|
|
3
3
|
export { default as DependencyLoader } from './lib/DependencyLoader.js'
|
|
4
4
|
export { default as Hook } from './lib/Hook.js'
|
|
5
|
-
export { metadataFileName, packageFileName, isObject, getArgs, spawn, readJson, writeJson, toBoolean, ensureDir, escapeRegExp } from './lib/Utils.js'
|
|
5
|
+
export { metadataFileName, packageFileName, isObject, getArgs, spawn, readJson, writeJson, toBoolean, ensureDir, escapeRegExp, stringifyValues } from './lib/Utils.js'
|
package/lib/Utils.js
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Clones an object and converts any Dates and ObjectIds to Strings
|
|
3
|
+
* @param {Object} data
|
|
4
|
+
* @returns A clone object with stringified ObjectIds
|
|
5
|
+
* @memberof core
|
|
6
|
+
*/
|
|
7
|
+
export function stringifyValues (data) {
|
|
8
|
+
return Object.entries(data).reduce((cloned, [key, val]) => {
|
|
9
|
+
const type = val?.constructor?.name
|
|
10
|
+
cloned[key] =
|
|
11
|
+
type === 'Date' || type === 'ObjectId'
|
|
12
|
+
? val.toString()
|
|
13
|
+
: type === 'Array' || type === 'Object'
|
|
14
|
+
? stringifyValues(val)
|
|
15
|
+
: val
|
|
16
|
+
return cloned
|
|
17
|
+
}, Array.isArray(data) ? [] : {})
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { describe, it } from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { stringifyValues } from '../lib/utils/stringifyValues.js'
|
|
4
|
+
|
|
5
|
+
describe('stringifyValues()', () => {
|
|
6
|
+
it('should pass through plain values unchanged', () => {
|
|
7
|
+
const data = { a: 'hello', b: 42, c: true, d: null }
|
|
8
|
+
assert.deepEqual(stringifyValues(data), data)
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
it('should convert Date values to strings', () => {
|
|
12
|
+
const date = new Date('2025-01-01T00:00:00.000Z')
|
|
13
|
+
const result = stringifyValues({ date })
|
|
14
|
+
assert.equal(typeof result.date, 'string')
|
|
15
|
+
assert.equal(result.date, date.toString())
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('should convert ObjectId-like values to strings', () => {
|
|
19
|
+
class ObjectId {
|
|
20
|
+
constructor (id) { this.id = id }
|
|
21
|
+
toString () { return this.id }
|
|
22
|
+
}
|
|
23
|
+
const data = { _id: new ObjectId('abc123') }
|
|
24
|
+
const result = stringifyValues(data)
|
|
25
|
+
assert.equal(typeof result._id, 'string')
|
|
26
|
+
assert.equal(result._id, 'abc123')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should recursively process nested objects', () => {
|
|
30
|
+
const date = new Date('2025-01-01')
|
|
31
|
+
const data = { nested: { value: 'test', date } }
|
|
32
|
+
const result = stringifyValues(data)
|
|
33
|
+
assert.equal(typeof result.nested, 'object')
|
|
34
|
+
assert.equal(typeof result.nested.date, 'string')
|
|
35
|
+
assert.equal(result.nested.value, 'test')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should recursively process arrays', () => {
|
|
39
|
+
const date = new Date('2025-01-01')
|
|
40
|
+
const data = { items: [date, 'text', 42] }
|
|
41
|
+
const result = stringifyValues(data)
|
|
42
|
+
assert.ok(Array.isArray(result.items))
|
|
43
|
+
assert.equal(typeof result.items[0], 'string')
|
|
44
|
+
assert.equal(result.items[1], 'text')
|
|
45
|
+
assert.equal(result.items[2], 42)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('should return an array when input is an array', () => {
|
|
49
|
+
const result = stringifyValues([{ a: 1 }, { b: 2 }])
|
|
50
|
+
assert.ok(Array.isArray(result))
|
|
51
|
+
assert.equal(result.length, 2)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('should not mutate the original data', () => {
|
|
55
|
+
const date = new Date('2025-01-01')
|
|
56
|
+
const data = { date }
|
|
57
|
+
stringifyValues(data)
|
|
58
|
+
assert.ok(data.date instanceof Date)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('should handle deeply nested structures', () => {
|
|
62
|
+
const date = new Date('2025-01-01')
|
|
63
|
+
const data = { a: { b: { c: { date } } } }
|
|
64
|
+
const result = stringifyValues(data)
|
|
65
|
+
assert.equal(typeof result.a.b.c.date, 'string')
|
|
66
|
+
})
|
|
67
|
+
})
|