@cloudpss/yaml 0.4.13 → 0.4.15

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.
@@ -0,0 +1,196 @@
1
+ /* eslint-disable */
2
+ var addSorting = (function() {
3
+ 'use strict';
4
+ var cols,
5
+ currentSort = {
6
+ index: 0,
7
+ desc: false
8
+ };
9
+
10
+ // returns the summary table element
11
+ function getTable() {
12
+ return document.querySelector('.coverage-summary');
13
+ }
14
+ // returns the thead element of the summary table
15
+ function getTableHeader() {
16
+ return getTable().querySelector('thead tr');
17
+ }
18
+ // returns the tbody element of the summary table
19
+ function getTableBody() {
20
+ return getTable().querySelector('tbody');
21
+ }
22
+ // returns the th element for nth column
23
+ function getNthColumn(n) {
24
+ return getTableHeader().querySelectorAll('th')[n];
25
+ }
26
+
27
+ function onFilterInput() {
28
+ const searchValue = document.getElementById('fileSearch').value;
29
+ const rows = document.getElementsByTagName('tbody')[0].children;
30
+ for (let i = 0; i < rows.length; i++) {
31
+ const row = rows[i];
32
+ if (
33
+ row.textContent
34
+ .toLowerCase()
35
+ .includes(searchValue.toLowerCase())
36
+ ) {
37
+ row.style.display = '';
38
+ } else {
39
+ row.style.display = 'none';
40
+ }
41
+ }
42
+ }
43
+
44
+ // loads the search box
45
+ function addSearchBox() {
46
+ var template = document.getElementById('filterTemplate');
47
+ var templateClone = template.content.cloneNode(true);
48
+ templateClone.getElementById('fileSearch').oninput = onFilterInput;
49
+ template.parentElement.appendChild(templateClone);
50
+ }
51
+
52
+ // loads all columns
53
+ function loadColumns() {
54
+ var colNodes = getTableHeader().querySelectorAll('th'),
55
+ colNode,
56
+ cols = [],
57
+ col,
58
+ i;
59
+
60
+ for (i = 0; i < colNodes.length; i += 1) {
61
+ colNode = colNodes[i];
62
+ col = {
63
+ key: colNode.getAttribute('data-col'),
64
+ sortable: !colNode.getAttribute('data-nosort'),
65
+ type: colNode.getAttribute('data-type') || 'string'
66
+ };
67
+ cols.push(col);
68
+ if (col.sortable) {
69
+ col.defaultDescSort = col.type === 'number';
70
+ colNode.innerHTML =
71
+ colNode.innerHTML + '<span class="sorter"></span>';
72
+ }
73
+ }
74
+ return cols;
75
+ }
76
+ // attaches a data attribute to every tr element with an object
77
+ // of data values keyed by column name
78
+ function loadRowData(tableRow) {
79
+ var tableCols = tableRow.querySelectorAll('td'),
80
+ colNode,
81
+ col,
82
+ data = {},
83
+ i,
84
+ val;
85
+ for (i = 0; i < tableCols.length; i += 1) {
86
+ colNode = tableCols[i];
87
+ col = cols[i];
88
+ val = colNode.getAttribute('data-value');
89
+ if (col.type === 'number') {
90
+ val = Number(val);
91
+ }
92
+ data[col.key] = val;
93
+ }
94
+ return data;
95
+ }
96
+ // loads all row data
97
+ function loadData() {
98
+ var rows = getTableBody().querySelectorAll('tr'),
99
+ i;
100
+
101
+ for (i = 0; i < rows.length; i += 1) {
102
+ rows[i].data = loadRowData(rows[i]);
103
+ }
104
+ }
105
+ // sorts the table using the data for the ith column
106
+ function sortByIndex(index, desc) {
107
+ var key = cols[index].key,
108
+ sorter = function(a, b) {
109
+ a = a.data[key];
110
+ b = b.data[key];
111
+ return a < b ? -1 : a > b ? 1 : 0;
112
+ },
113
+ finalSorter = sorter,
114
+ tableBody = document.querySelector('.coverage-summary tbody'),
115
+ rowNodes = tableBody.querySelectorAll('tr'),
116
+ rows = [],
117
+ i;
118
+
119
+ if (desc) {
120
+ finalSorter = function(a, b) {
121
+ return -1 * sorter(a, b);
122
+ };
123
+ }
124
+
125
+ for (i = 0; i < rowNodes.length; i += 1) {
126
+ rows.push(rowNodes[i]);
127
+ tableBody.removeChild(rowNodes[i]);
128
+ }
129
+
130
+ rows.sort(finalSorter);
131
+
132
+ for (i = 0; i < rows.length; i += 1) {
133
+ tableBody.appendChild(rows[i]);
134
+ }
135
+ }
136
+ // removes sort indicators for current column being sorted
137
+ function removeSortIndicators() {
138
+ var col = getNthColumn(currentSort.index),
139
+ cls = col.className;
140
+
141
+ cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
142
+ col.className = cls;
143
+ }
144
+ // adds sort indicators for current column being sorted
145
+ function addSortIndicators() {
146
+ getNthColumn(currentSort.index).className += currentSort.desc
147
+ ? ' sorted-desc'
148
+ : ' sorted';
149
+ }
150
+ // adds event listeners for all sorter widgets
151
+ function enableUI() {
152
+ var i,
153
+ el,
154
+ ithSorter = function ithSorter(i) {
155
+ var col = cols[i];
156
+
157
+ return function() {
158
+ var desc = col.defaultDescSort;
159
+
160
+ if (currentSort.index === i) {
161
+ desc = !currentSort.desc;
162
+ }
163
+ sortByIndex(i, desc);
164
+ removeSortIndicators();
165
+ currentSort.index = i;
166
+ currentSort.desc = desc;
167
+ addSortIndicators();
168
+ };
169
+ };
170
+ for (i = 0; i < cols.length; i += 1) {
171
+ if (cols[i].sortable) {
172
+ // add the click event handler on the th so users
173
+ // dont have to click on those tiny arrows
174
+ el = getNthColumn(i).querySelector('.sorter').parentElement;
175
+ if (el.addEventListener) {
176
+ el.addEventListener('click', ithSorter(i));
177
+ } else {
178
+ el.attachEvent('onclick', ithSorter(i));
179
+ }
180
+ }
181
+ }
182
+ }
183
+ // adds sorting functionality to the UI
184
+ return function() {
185
+ if (!getTable()) {
186
+ return;
187
+ }
188
+ cols = loadColumns();
189
+ loadData();
190
+ addSearchBox();
191
+ addSortIndicators();
192
+ enableUI();
193
+ };
194
+ })();
195
+
196
+ window.addEventListener('load', addSorting);
@@ -0,0 +1,179 @@
1
+ TN:
2
+ SF:src\index.ts
3
+ FN:7,load
4
+ FN:14,dump
5
+ FNF:2
6
+ FNH:2
7
+ FNDA:30,load
8
+ FNDA:30,dump
9
+ DA:1,1
10
+ DA:2,1
11
+ DA:3,1
12
+ DA:4,1
13
+ DA:5,1
14
+ DA:6,1
15
+ DA:7,1
16
+ DA:8,30
17
+ DA:9,30
18
+ DA:10,30
19
+ DA:11,30
20
+ DA:12,30
21
+ DA:13,1
22
+ DA:14,1
23
+ DA:15,30
24
+ DA:16,30
25
+ DA:17,30
26
+ DA:18,30
27
+ DA:19,30
28
+ DA:20,30
29
+ DA:21,30
30
+ DA:22,30
31
+ DA:23,30
32
+ DA:24,1
33
+ LF:24
34
+ LH:24
35
+ BRDA:7,0,0,30
36
+ BRDA:14,1,0,30
37
+ BRF:2
38
+ BRH:2
39
+ end_of_record
40
+ TN:
41
+ SF:src\schema.js
42
+ FN:6,isBase64
43
+ FN:14,construct
44
+ FN:16,represent
45
+ FN:33,resolve
46
+ FN:39,construct
47
+ FN:49,base64
48
+ FN:55,sequence
49
+ FN:75,construct
50
+ FN:77,represent
51
+ FN:81,construct
52
+ FN:83,represent
53
+ FNF:11
54
+ FNH:11
55
+ FNDA:19,isBase64
56
+ FNDA:1,construct
57
+ FNDA:1,represent
58
+ FNDA:27,resolve
59
+ FNDA:27,construct
60
+ FNDA:18,base64
61
+ FNDA:9,sequence
62
+ FNDA:1,construct
63
+ FNDA:1,represent
64
+ FNDA:1,construct
65
+ FNDA:1,represent
66
+ DA:1,1
67
+ DA:2,1
68
+ DA:3,1
69
+ DA:4,1
70
+ DA:5,1
71
+ DA:6,19
72
+ DA:7,19
73
+ DA:8,19
74
+ DA:9,19
75
+ DA:10,1
76
+ DA:11,1
77
+ DA:12,1
78
+ DA:13,1
79
+ DA:14,1
80
+ DA:15,1
81
+ DA:16,1
82
+ DA:17,1
83
+ DA:18,1
84
+ DA:19,1
85
+ DA:20,1
86
+ DA:21,1
87
+ DA:22,1
88
+ DA:23,1
89
+ DA:24,1
90
+ DA:25,1
91
+ DA:26,1
92
+ DA:27,1
93
+ DA:28,1
94
+ DA:29,1
95
+ DA:30,9
96
+ DA:31,9
97
+ DA:32,9
98
+ DA:33,9
99
+ DA:34,27
100
+ DA:35,9
101
+ DA:36,9
102
+ DA:37,18
103
+ DA:38,9
104
+ DA:39,9
105
+ DA:40,27
106
+ DA:41,9
107
+ DA:42,9
108
+ DA:43,27
109
+ DA:44,27
110
+ DA:45,9
111
+ DA:46,9
112
+ DA:47,9
113
+ DA:48,9
114
+ DA:49,9
115
+ DA:50,18
116
+ DA:51,18
117
+ DA:52,18
118
+ DA:53,18
119
+ DA:54,9
120
+ DA:55,9
121
+ DA:56,9
122
+ DA:57,9
123
+ DA:58,9
124
+ DA:59,9
125
+ DA:60,9
126
+ DA:61,9
127
+ DA:62,9
128
+ DA:63,9
129
+ DA:64,9
130
+ DA:65,9
131
+ DA:66,9
132
+ DA:67,9
133
+ DA:68,9
134
+ DA:69,9
135
+ DA:70,9
136
+ DA:71,9
137
+ DA:72,1
138
+ DA:73,1
139
+ DA:74,1
140
+ DA:75,1
141
+ DA:76,1
142
+ DA:77,1
143
+ DA:78,1
144
+ DA:79,1
145
+ DA:80,1
146
+ DA:81,1
147
+ DA:82,1
148
+ DA:83,1
149
+ DA:84,1
150
+ DA:85,1
151
+ DA:86,1
152
+ DA:87,1
153
+ DA:88,1
154
+ LF:88
155
+ LH:88
156
+ BRDA:6,0,0,19
157
+ BRDA:7,1,0,0
158
+ BRDA:14,2,0,1
159
+ BRDA:14,3,0,0
160
+ BRDA:16,4,0,1
161
+ BRDA:29,5,0,9
162
+ BRDA:33,6,0,27
163
+ BRDA:34,7,0,9
164
+ BRDA:37,8,0,18
165
+ BRDA:39,9,0,27
166
+ BRDA:40,10,0,9
167
+ BRDA:43,11,0,18
168
+ BRDA:43,12,0,0
169
+ BRDA:49,13,0,18
170
+ BRDA:55,14,0,9
171
+ BRDA:75,15,0,1
172
+ BRDA:75,16,0,0
173
+ BRDA:77,17,0,1
174
+ BRDA:81,18,0,1
175
+ BRDA:81,19,0,0
176
+ BRDA:83,20,0,1
177
+ BRF:21
178
+ BRH:16
179
+ end_of_record
package/dist/index.js CHANGED
@@ -9,13 +9,29 @@ export function load(str, opts) {
9
9
  }
10
10
  /** 序列化为 YAML */
11
11
  export function dump(obj, opts) {
12
- return _dump(obj, {
12
+ const resolvedOpts = {
13
13
  lineWidth: 200,
14
14
  flowLevel: 3,
15
15
  noRefs: true,
16
16
  ...opts,
17
17
  schema: CLOUDPSS_SCHEMA,
18
18
  skipInvalid: true,
19
- });
19
+ };
20
+ if (resolvedOpts.styles?.['!!js/TypedArray'] != null) {
21
+ const style = resolvedOpts.styles['!!js/TypedArray'];
22
+ resolvedOpts.styles = {
23
+ '!!js/Float32Array': style,
24
+ '!!js/Float64Array': style,
25
+ '!!js/Uint8Array': style,
26
+ '!!js/Uint8ClampedArray': style,
27
+ '!!js/Uint16Array': style,
28
+ '!!js/Uint32Array': style,
29
+ '!!js/Int8Array': style,
30
+ '!!js/Int16Array': style,
31
+ '!!js/Int32Array': style,
32
+ ...resolvedOpts.styles,
33
+ };
34
+ }
35
+ return _dump(obj, resolvedOpts);
20
36
  }
21
37
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,EAAE,MAAM,SAAS,CAAC;AAGvD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,cAAc;AACd,MAAM,UAAU,IAAI,CAAC,GAAW,EAAE,IAAkB;IAChD,OAAO,KAAK,CAAC,GAAG,EAAE;QACd,GAAG,IAAI;QACP,MAAM,EAAE,eAAe;KAC1B,CAAC,CAAC;AACP,CAAC;AACD,gBAAgB;AAChB,MAAM,UAAU,IAAI,CAAC,GAAY,EAAE,IAAkB;IACjD,OAAO,KAAK,CAAC,GAAG,EAAE;QACd,SAAS,EAAE,GAAG;QACd,SAAS,EAAE,CAAC;QACZ,MAAM,EAAE,IAAI;QACZ,GAAG,IAAI;QACP,MAAM,EAAE,eAAe;QACvB,WAAW,EAAE,IAAI;KACpB,CAAC,CAAC;AACP,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,IAAI,KAAK,EAAE,IAAI,IAAI,KAAK,EAAE,MAAM,SAAS,CAAC;AAGvD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,cAAc;AACd,MAAM,UAAU,IAAI,CAAC,GAAW,EAAE,IAAkB;IAChD,OAAO,KAAK,CAAC,GAAG,EAAE;QACd,GAAG,IAAI;QACP,MAAM,EAAE,eAAe;KAC1B,CAAC,CAAC;AACP,CAAC;AACD,gBAAgB;AAChB,MAAM,UAAU,IAAI,CAAC,GAAY,EAAE,IAAkB;IACjD,MAAM,YAAY,GAAgB;QAC9B,SAAS,EAAE,GAAG;QACd,SAAS,EAAE,CAAC;QACZ,MAAM,EAAE,IAAI;QACZ,GAAG,IAAI;QACP,MAAM,EAAE,eAAe;QACvB,WAAW,EAAE,IAAI;KACpB,CAAC;IACF,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC,iBAAiB,CAAC,IAAI,IAAI,EAAE;QAClD,MAAM,KAAK,GAAY,YAAY,CAAC,MAAM,CAAC,iBAAiB,CAAC,CAAC;QAC9D,YAAY,CAAC,MAAM,GAAG;YAClB,mBAAmB,EAAE,KAAK;YAC1B,mBAAmB,EAAE,KAAK;YAC1B,iBAAiB,EAAE,KAAK;YACxB,wBAAwB,EAAE,KAAK;YAC/B,kBAAkB,EAAE,KAAK;YACzB,kBAAkB,EAAE,KAAK;YACzB,gBAAgB,EAAE,KAAK;YACvB,iBAAiB,EAAE,KAAK;YACxB,iBAAiB,EAAE,KAAK;YACxB,GAAG,YAAY,CAAC,MAAM;SACzB,CAAC;KACL;IACD,OAAO,KAAK,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;AACpC,CAAC"}
package/dist/schema.js CHANGED
@@ -1,8 +1,14 @@
1
- import { Type, DEFAULT_SCHEMA } from 'js-yaml';
1
+ import { Type, DEFAULT_SCHEMA, CORE_SCHEMA } from 'js-yaml';
2
2
  import { toUint8Array, fromUint8Array } from 'js-base64';
3
+ /** Check if data is base64 encoded */
4
+ function isBase64(data) {
5
+ if (typeof data !== 'string')
6
+ return false;
7
+ return /^[A-Za-z0-9+/]*={0,2}$/.test(data);
8
+ }
3
9
  const buffer = new Type('tag:yaml.org,2002:js/ArrayBuffer', {
4
10
  kind: 'scalar',
5
- resolve: (data) => data != null,
11
+ resolve: isBase64,
6
12
  construct: (/** @type {string} */ data) => toUint8Array(data || '').buffer,
7
13
  instanceOf: ArrayBuffer,
8
14
  represent: (object) => fromUint8Array(new Uint8Array(object)),
@@ -17,20 +23,48 @@ const typedArrays = [
17
23
  Int8Array,
18
24
  Int16Array,
19
25
  Int32Array,
20
- ].map((c) => new Type(`tag:yaml.org,2002:js/${c.name}`, {
21
- kind: 'scalar',
22
- resolve: (data) => data != null,
23
- construct: (/** @type {string} */ data) => {
24
- const buf = toUint8Array(data || '');
25
- return new c(buf.buffer, buf.byteOffset, buf.byteLength / c.BYTES_PER_ELEMENT);
26
- },
27
- instanceOf: c,
28
- represent: (
29
- /** @type {Float32Array|Float64Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array} */ object) => {
30
- const buf = new Uint8Array(object.buffer, object.byteOffset, object.byteLength);
31
- return fromUint8Array(buf);
32
- },
33
- }));
26
+ ].flatMap((c) => {
27
+ const yamlType = `tag:yaml.org,2002:js/${c.name}`;
28
+ /** @type {import('js-yaml').TypeConstructorOptions} */
29
+ const commonInit = {
30
+ resolve: (/** @type {string | number[]} */ data) => {
31
+ if (Array.isArray(data)) {
32
+ return true;
33
+ }
34
+ return isBase64(data);
35
+ },
36
+ construct: (/** @type {string | number[]} */ data) => {
37
+ if (Array.isArray(data)) {
38
+ return c.from(data);
39
+ }
40
+ const buf = toUint8Array(data || '');
41
+ return new c(buf.buffer, buf.byteOffset, buf.byteLength / c.BYTES_PER_ELEMENT);
42
+ },
43
+ instanceOf: c,
44
+ defaultStyle: 'base64',
45
+ represent: {
46
+ base64: (
47
+ /** @type {Float32Array|Float64Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array} */ object) => {
48
+ const buf = new Uint8Array(object.buffer, object.byteOffset, object.byteLength);
49
+ return fromUint8Array(buf);
50
+ },
51
+ sequence: (
52
+ /** @type {Float32Array|Float64Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array} */ object) => {
53
+ return [...object];
54
+ },
55
+ },
56
+ };
57
+ return [
58
+ new Type(yamlType, {
59
+ kind: 'scalar',
60
+ ...commonInit,
61
+ }),
62
+ new Type(yamlType, {
63
+ kind: 'sequence',
64
+ ...commonInit,
65
+ }),
66
+ ];
67
+ });
34
68
  const map = new Type('tag:yaml.org,2002:js/map', {
35
69
  kind: 'sequence',
36
70
  construct: (/** @type {[unknown, unknown][]} */ data) => (data == null ? new Map() : new Map(data)),
@@ -43,7 +77,7 @@ const set = new Type('tag:yaml.org,2002:js/set', {
43
77
  instanceOf: Set,
44
78
  represent: (/** @type {Set<unknown>} */ object) => [...object],
45
79
  });
46
- export const CLOUDPSS_SCHEMA = DEFAULT_SCHEMA.extend({
80
+ export const CLOUDPSS_SCHEMA = CORE_SCHEMA.extend({
47
81
  explicit: [buffer, ...typedArrays, map, set],
48
- });
82
+ }).extend(DEFAULT_SCHEMA);
49
83
  //# sourceMappingURL=schema.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAE/C,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEzD,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,kCAAkC,EAAE;IACxD,IAAI,EAAE,QAAQ;IACd,OAAO,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,IAAI;IAC/B,SAAS,EAAE,CAAC,qBAAqB,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,MAAM;IAC1E,UAAU,E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AC,CAAC;AAEH,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAC,MAAM,CAAC;IAC9C,QAAQ,EAAE,CAAC,MAAM,EAAE,GAAG,WAAW,EAAE,GAAG,EAAE,GAAG,CAAC;CAC/C,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC"}
package/jest.config.js ADDED
@@ -0,0 +1,3 @@
1
+ import { config } from '../../jest.config.js';
2
+
3
+ export default { ...config };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudpss/yaml",
3
- "version": "0.4.13",
3
+ "version": "0.4.15",
4
4
  "author": "CloudPSS",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -16,7 +16,8 @@
16
16
  "build": "yarn clean && tsc",
17
17
  "prepublishOnly": "yarn build",
18
18
  "clean": "rimraf dist",
19
- "benchmark": "node ./benchmark"
19
+ "benchmark": "node ./benchmark",
20
+ "test": "cross-env NODE_OPTIONS=--experimental-vm-modules jest"
20
21
  },
21
22
  "dependencies": {
22
23
  "@types/js-yaml": "^4.0.5",
package/src/index.ts CHANGED
@@ -12,13 +12,29 @@ export function load(str: string, opts?: LoadOptions): unknown {
12
12
  }
13
13
  /** 序列化为 YAML */
14
14
  export function dump(obj: unknown, opts?: DumpOptions): string {
15
- return _dump(obj, {
15
+ const resolvedOpts: DumpOptions = {
16
16
  lineWidth: 200,
17
17
  flowLevel: 3,
18
18
  noRefs: true,
19
19
  ...opts,
20
20
  schema: CLOUDPSS_SCHEMA,
21
21
  skipInvalid: true,
22
- });
22
+ };
23
+ if (resolvedOpts.styles?.['!!js/TypedArray'] != null) {
24
+ const style: unknown = resolvedOpts.styles['!!js/TypedArray'];
25
+ resolvedOpts.styles = {
26
+ '!!js/Float32Array': style,
27
+ '!!js/Float64Array': style,
28
+ '!!js/Uint8Array': style,
29
+ '!!js/Uint8ClampedArray': style,
30
+ '!!js/Uint16Array': style,
31
+ '!!js/Uint32Array': style,
32
+ '!!js/Int8Array': style,
33
+ '!!js/Int16Array': style,
34
+ '!!js/Int32Array': style,
35
+ ...resolvedOpts.styles,
36
+ };
37
+ }
38
+ return _dump(obj, resolvedOpts);
23
39
  }
24
40
  export type { LoadOptions, DumpOptions };
package/src/schema.js CHANGED
@@ -1,10 +1,16 @@
1
- import { Type, DEFAULT_SCHEMA } from 'js-yaml';
1
+ import { Type, DEFAULT_SCHEMA, CORE_SCHEMA } from 'js-yaml';
2
2
 
3
3
  import { toUint8Array, fromUint8Array } from 'js-base64';
4
4
 
5
+ /** Check if data is base64 encoded */
6
+ function isBase64(data) {
7
+ if (typeof data !== 'string') return false;
8
+ return /^[A-Za-z0-9+/]*={0,2}$/.test(data);
9
+ }
10
+
5
11
  const buffer = new Type('tag:yaml.org,2002:js/ArrayBuffer', {
6
12
  kind: 'scalar',
7
- resolve: (data) => data != null,
13
+ resolve: isBase64,
8
14
  construct: (/** @type {string} */ data) => toUint8Array(data || '').buffer,
9
15
  instanceOf: ArrayBuffer,
10
16
  represent: (object) => fromUint8Array(new Uint8Array(object)),
@@ -20,24 +26,50 @@ const typedArrays = [
20
26
  Int8Array,
21
27
  Int16Array,
22
28
  Int32Array,
23
- ].map(
24
- (c) =>
25
- new Type(`tag:yaml.org,2002:js/${c.name}`, {
26
- kind: 'scalar',
27
- resolve: (data) => data != null,
28
- construct: (/** @type {string} */ data) => {
29
- const buf = toUint8Array(data || '');
30
- return new c(buf.buffer, buf.byteOffset, buf.byteLength / c.BYTES_PER_ELEMENT);
31
- },
32
- instanceOf: c,
33
- represent: (
29
+ ].flatMap((c) => {
30
+ const yamlType = `tag:yaml.org,2002:js/${c.name}`;
31
+ /** @type {import('js-yaml').TypeConstructorOptions} */
32
+ const commonInit = {
33
+ resolve: (/** @type {string | number[]} */ data) => {
34
+ if (Array.isArray(data)) {
35
+ return true;
36
+ }
37
+ return isBase64(data);
38
+ },
39
+ construct: (/** @type {string | number[]} */ data) => {
40
+ if (Array.isArray(data)) {
41
+ return c.from(data);
42
+ }
43
+ const buf = toUint8Array(data || '');
44
+ return new c(buf.buffer, buf.byteOffset, buf.byteLength / c.BYTES_PER_ELEMENT);
45
+ },
46
+ instanceOf: c,
47
+ defaultStyle: 'base64',
48
+ represent: {
49
+ base64: (
34
50
  /** @type {Float32Array|Float64Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array} */ object,
35
51
  ) => {
36
52
  const buf = new Uint8Array(object.buffer, object.byteOffset, object.byteLength);
37
53
  return fromUint8Array(buf);
38
54
  },
55
+ sequence: (
56
+ /** @type {Float32Array|Float64Array|Uint8ClampedArray|Uint16Array|Uint32Array|Int8Array|Int16Array|Int32Array} */ object,
57
+ ) => {
58
+ return [...object];
59
+ },
60
+ },
61
+ };
62
+ return [
63
+ new Type(yamlType, {
64
+ kind: 'scalar',
65
+ ...commonInit,
66
+ }),
67
+ new Type(yamlType, {
68
+ kind: 'sequence',
69
+ ...commonInit,
39
70
  }),
40
- );
71
+ ];
72
+ });
41
73
  const map = new Type('tag:yaml.org,2002:js/map', {
42
74
  kind: 'sequence',
43
75
  construct: (/** @type {[unknown, unknown][]} */ data) => (data == null ? new Map() : new Map(data)),
@@ -51,6 +83,6 @@ const set = new Type('tag:yaml.org,2002:js/set', {
51
83
  represent: (/** @type {Set<unknown>} */ object) => [...object],
52
84
  });
53
85
 
54
- export const CLOUDPSS_SCHEMA = DEFAULT_SCHEMA.extend({
86
+ export const CLOUDPSS_SCHEMA = CORE_SCHEMA.extend({
55
87
  explicit: [buffer, ...typedArrays, map, set],
56
- });
88
+ }).extend(DEFAULT_SCHEMA);