@squiz/dx-common-lib 1.21.1-alpha.7 → 1.22.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/.npm/_logs/2023-03-29T01_26_08_740Z-debug-0.log +37 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/json-order/index.d.ts +6 -0
- package/lib/json-order/index.js +11 -0
- package/lib/json-order/index.js.map +1 -0
- package/lib/json-order/key.d.ts +2 -0
- package/lib/json-order/key.js +44 -0
- package/lib/json-order/key.js.map +1 -0
- package/lib/json-order/key.spec.d.ts +1 -0
- package/lib/json-order/key.spec.js +40 -0
- package/lib/json-order/key.spec.js.map +1 -0
- package/lib/json-order/models.d.ts +7 -0
- package/lib/json-order/models.js +3 -0
- package/lib/json-order/models.js.map +1 -0
- package/lib/json-order/order.d.ts +11 -0
- package/lib/json-order/order.js +75 -0
- package/lib/json-order/order.js.map +1 -0
- package/lib/json-order/order.spec.d.ts +1 -0
- package/lib/json-order/order.spec.js +191 -0
- package/lib/json-order/order.spec.js.map +1 -0
- package/lib/json-order/parse.d.ts +11 -0
- package/lib/json-order/parse.js +47 -0
- package/lib/json-order/parse.js.map +1 -0
- package/lib/json-order/parse.spec.d.ts +1 -0
- package/lib/json-order/parse.spec.js +260 -0
- package/lib/json-order/parse.spec.js.map +1 -0
- package/lib/json-order/stringify.d.ts +12 -0
- package/lib/json-order/stringify.js +20 -0
- package/lib/json-order/stringify.js.map +1 -0
- package/lib/json-order/stringify.spec.d.ts +1 -0
- package/lib/json-order/stringify.spec.js +123 -0
- package/lib/json-order/stringify.spec.js.map +1 -0
- package/lib/server-utils/errorMiddleware.js +1 -0
- package/lib/server-utils/errorMiddleware.js.map +1 -1
- package/lib/server-utils/errorMiddleware.spec.js +1 -0
- package/lib/server-utils/errorMiddleware.spec.js.map +1 -1
- package/package.json +9 -6
- package/src/index.ts +1 -0
- package/src/json-order/index.ts +6 -0
- package/src/json-order/key.spec.ts +39 -0
- package/src/json-order/key.ts +40 -0
- package/src/json-order/models.ts +8 -0
- package/src/json-order/order.spec.ts +251 -0
- package/src/json-order/order.ts +95 -0
- package/src/json-order/parse.spec.ts +302 -0
- package/src/json-order/parse.ts +59 -0
- package/src/json-order/stringify.spec.ts +170 -0
- package/src/json-order/stringify.ts +17 -0
- package/src/server-utils/errorMiddleware.spec.ts +1 -0
- package/src/server-utils/errorMiddleware.ts +2 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/.npm/_logs/2023-03-08T00_07_18_460Z-debug-0.log +0 -39
@@ -0,0 +1,170 @@
|
|
1
|
+
import stringify from './stringify';
|
2
|
+
import { PropertyMap } from './models';
|
3
|
+
|
4
|
+
describe('stringify ', () => {
|
5
|
+
const expectString = (obj: object, map: PropertyMap | null, str: string) =>
|
6
|
+
expect(stringify(obj, map, '.', 0)).toEqual(str);
|
7
|
+
|
8
|
+
it('returns nothing for a blank JSON string', () => expectString({}, {}, '{}'));
|
9
|
+
|
10
|
+
it('throws error if separator is an empty string', () => {
|
11
|
+
expect(() => stringify({}, {}, '')).toThrowError('Separator should not be an empty string.');
|
12
|
+
});
|
13
|
+
|
14
|
+
it('ignores properties not found in source', () => expectString({}, { $: ['a'] }, '{}'));
|
15
|
+
|
16
|
+
it('returns regular json string if map is undefined', () =>
|
17
|
+
expectString({ a: '1', b: '2' }, null, '{"a":"1","b":"2"}'));
|
18
|
+
|
19
|
+
it('ignores properties not found in map', () => expectString({ a: '1', b: '2' }, { $: ['b'] }, '{"b":"2"}'));
|
20
|
+
|
21
|
+
it('returns first level object properties in order', () =>
|
22
|
+
expectString({ a: 2, b: 1 }, { $: ['b', 'a'] }, '{"b":1,"a":2}'));
|
23
|
+
|
24
|
+
it('returns first level array value in order', () =>
|
25
|
+
expectString({ a: ['2', 1, true] }, { $: ['a'] }, '{"a":["2",1,true]}'));
|
26
|
+
|
27
|
+
it('returns nested [array] > [object] properties in expected order', () =>
|
28
|
+
expectString({ a: [1, { c: '3', d: '2' }] }, { $: ['a'], '$.a.1': ['d', 'c'] }, '{"a":[1,{"d":"2","c":"3"}]}'));
|
29
|
+
|
30
|
+
it('ignores nested [array] > [object] properties partially not found in map', () =>
|
31
|
+
expectString({ a: [1, { b: 2, c: 3 }, 4] }, { $: ['a'], '$.a.1': ['c'] }, '{"a":[1,{"c":3},4]}'));
|
32
|
+
|
33
|
+
it('ignores nested [array] > [object] properties not found in map', () =>
|
34
|
+
expectString({ a: [1, { b: 2, c: 3 }, 4] }, { $: ['a'] }, '{"a":[1,{},4]}'));
|
35
|
+
|
36
|
+
it('handles multi-character prefix', () => {
|
37
|
+
expect(
|
38
|
+
stringify(
|
39
|
+
{
|
40
|
+
a: {
|
41
|
+
b: {
|
42
|
+
c: 3,
|
43
|
+
d: 4,
|
44
|
+
},
|
45
|
+
e: {
|
46
|
+
f: 4,
|
47
|
+
g: 5,
|
48
|
+
},
|
49
|
+
h: 6,
|
50
|
+
},
|
51
|
+
i: 7,
|
52
|
+
},
|
53
|
+
{
|
54
|
+
ab: ['i', 'a'],
|
55
|
+
'ab.a': ['e', 'h', 'b'],
|
56
|
+
'ab.a.e': ['g', 'f'],
|
57
|
+
'ab.a.b': ['d', 'c'],
|
58
|
+
},
|
59
|
+
'.',
|
60
|
+
0,
|
61
|
+
),
|
62
|
+
).toEqual('{"i":7,"a":{"e":{"g":5,"f":4},"h":6,"b":{"d":4,"c":3}}}');
|
63
|
+
});
|
64
|
+
|
65
|
+
it('handles multi-character separator', () => {
|
66
|
+
expect(
|
67
|
+
stringify(
|
68
|
+
{
|
69
|
+
a: {
|
70
|
+
b: {
|
71
|
+
c: 3,
|
72
|
+
d: 4,
|
73
|
+
},
|
74
|
+
e: {
|
75
|
+
f: 4,
|
76
|
+
g: 5,
|
77
|
+
},
|
78
|
+
h: 6,
|
79
|
+
},
|
80
|
+
i: 7,
|
81
|
+
},
|
82
|
+
{
|
83
|
+
$: ['i', 'a'],
|
84
|
+
'$~|a': ['e', 'h', 'b'],
|
85
|
+
'$~|a~|e': ['g', 'f'],
|
86
|
+
'$~|a~|b': ['d', 'c'],
|
87
|
+
},
|
88
|
+
'~|',
|
89
|
+
0,
|
90
|
+
),
|
91
|
+
).toEqual('{"i":7,"a":{"e":{"g":5,"f":4},"h":6,"b":{"d":4,"c":3}}}');
|
92
|
+
});
|
93
|
+
|
94
|
+
it('returns nested [object] > [object] properties in expected order', () =>
|
95
|
+
expectString(
|
96
|
+
{
|
97
|
+
a: {
|
98
|
+
b: {
|
99
|
+
c: 3,
|
100
|
+
d: 4,
|
101
|
+
},
|
102
|
+
e: {
|
103
|
+
f: 4,
|
104
|
+
g: 5,
|
105
|
+
},
|
106
|
+
h: 6,
|
107
|
+
},
|
108
|
+
i: 7,
|
109
|
+
},
|
110
|
+
{
|
111
|
+
$: ['i', 'a'],
|
112
|
+
'$.a': ['e', 'h', 'b'],
|
113
|
+
'$.a.e': ['g', 'f'],
|
114
|
+
'$.a.b': ['d', 'c'],
|
115
|
+
},
|
116
|
+
'{"i":7,"a":{"e":{"g":5,"f":4},"h":6,"b":{"d":4,"c":3}}}',
|
117
|
+
));
|
118
|
+
|
119
|
+
it('returns nested [object] > [array] > [object] > [array] > [object] properties in expected order', () =>
|
120
|
+
expectString(
|
121
|
+
{
|
122
|
+
a: {
|
123
|
+
b: [
|
124
|
+
8,
|
125
|
+
{
|
126
|
+
c: 9,
|
127
|
+
d: [
|
128
|
+
{
|
129
|
+
e: 12,
|
130
|
+
f: {
|
131
|
+
g: true,
|
132
|
+
h: 'h',
|
133
|
+
},
|
134
|
+
},
|
135
|
+
10,
|
136
|
+
],
|
137
|
+
},
|
138
|
+
11,
|
139
|
+
],
|
140
|
+
},
|
141
|
+
i: 7,
|
142
|
+
},
|
143
|
+
{
|
144
|
+
$: ['i', 'a'],
|
145
|
+
'$.a': ['b'],
|
146
|
+
'$.a.b.1': ['d', 'c'],
|
147
|
+
'$.a.b.1.d.0': ['f', 'e'],
|
148
|
+
'$.a.b.1.d.0.f': ['h', 'g'],
|
149
|
+
},
|
150
|
+
'{"i":7,"a":{"b":[8,{"d":[{"f":{"h":"h","g":true},"e":12},10],"c":9},11]}}',
|
151
|
+
));
|
152
|
+
|
153
|
+
it('handles keys with different types of values', () =>
|
154
|
+
expectString(
|
155
|
+
{
|
156
|
+
a: 'a',
|
157
|
+
b: 2,
|
158
|
+
c: 2.3,
|
159
|
+
d: true,
|
160
|
+
e: false,
|
161
|
+
f: null,
|
162
|
+
g: {},
|
163
|
+
h: [],
|
164
|
+
},
|
165
|
+
{
|
166
|
+
$: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'],
|
167
|
+
},
|
168
|
+
'{"a":"a","b":2,"c":2.3,"d":true,"e":false,"f":null,"g":{},"h":[]}',
|
169
|
+
));
|
170
|
+
});
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import { PropertyMap } from './models';
|
2
|
+
import order from './order';
|
3
|
+
|
4
|
+
/**
|
5
|
+
* Stringify a JS object while maintaining a particular property order:
|
6
|
+
*
|
7
|
+
* @param sourceObject an object with the properties in any order
|
8
|
+
* @param map the `PropertyMap` generated by the `parse` method.
|
9
|
+
* @param separator a non-empty `string` that controls what the key separator is in the generated map. Defaults to `~`.
|
10
|
+
* @param space a `number` used to insert white space into the output JSON string for readability purposes, as per the `JSON.stringify` [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters)
|
11
|
+
* @returns the stringified source object ordered as per the map. If the map is unset, the response is a standard `JSON.stringify`.
|
12
|
+
*/
|
13
|
+
const stringify = (sourceObject: object, map: PropertyMap | null, separator = '~', space?: number): string => {
|
14
|
+
return JSON.stringify(order(sourceObject, map, separator), null, space);
|
15
|
+
};
|
16
|
+
|
17
|
+
export default stringify;
|
@@ -5,6 +5,8 @@ export function errorMiddleware(error: any, req: Request, res: Response, _next:
|
|
5
5
|
const statusCode = error.statusCode || 500;
|
6
6
|
error.message = error?.message || 'An error occurred with no additional information available';
|
7
7
|
|
8
|
+
res = res.setHeader('cache-control', 'no-cache, no-store, max-age=0');
|
9
|
+
|
8
10
|
if (error?.name === 'ComponentError') {
|
9
11
|
req.log.error({
|
10
12
|
message: error,
|