1 | 'use strict';
|
---|
2 |
|
---|
3 | var createNode = require('../doc/createNode.js');
|
---|
4 | var stringifyCollection = require('../stringify/stringifyCollection.js');
|
---|
5 | var Collection = require('./Collection.js');
|
---|
6 | var identity = require('./identity.js');
|
---|
7 | var Scalar = require('./Scalar.js');
|
---|
8 | var toJS = require('./toJS.js');
|
---|
9 |
|
---|
10 | class YAMLSeq extends Collection.Collection {
|
---|
11 | static get tagName() {
|
---|
12 | return 'tag:yaml.org,2002:seq';
|
---|
13 | }
|
---|
14 | constructor(schema) {
|
---|
15 | super(identity.SEQ, schema);
|
---|
16 | this.items = [];
|
---|
17 | }
|
---|
18 | add(value) {
|
---|
19 | this.items.push(value);
|
---|
20 | }
|
---|
21 | /**
|
---|
22 | * Removes a value from the collection.
|
---|
23 | *
|
---|
24 | * `key` must contain a representation of an integer for this to succeed.
|
---|
25 | * It may be wrapped in a `Scalar`.
|
---|
26 | *
|
---|
27 | * @returns `true` if the item was found and removed.
|
---|
28 | */
|
---|
29 | delete(key) {
|
---|
30 | const idx = asItemIndex(key);
|
---|
31 | if (typeof idx !== 'number')
|
---|
32 | return false;
|
---|
33 | const del = this.items.splice(idx, 1);
|
---|
34 | return del.length > 0;
|
---|
35 | }
|
---|
36 | get(key, keepScalar) {
|
---|
37 | const idx = asItemIndex(key);
|
---|
38 | if (typeof idx !== 'number')
|
---|
39 | return undefined;
|
---|
40 | const it = this.items[idx];
|
---|
41 | return !keepScalar && identity.isScalar(it) ? it.value : it;
|
---|
42 | }
|
---|
43 | /**
|
---|
44 | * Checks if the collection includes a value with the key `key`.
|
---|
45 | *
|
---|
46 | * `key` must contain a representation of an integer for this to succeed.
|
---|
47 | * It may be wrapped in a `Scalar`.
|
---|
48 | */
|
---|
49 | has(key) {
|
---|
50 | const idx = asItemIndex(key);
|
---|
51 | return typeof idx === 'number' && idx < this.items.length;
|
---|
52 | }
|
---|
53 | /**
|
---|
54 | * Sets a value in this collection. For `!!set`, `value` needs to be a
|
---|
55 | * boolean to add/remove the item from the set.
|
---|
56 | *
|
---|
57 | * If `key` does not contain a representation of an integer, this will throw.
|
---|
58 | * It may be wrapped in a `Scalar`.
|
---|
59 | */
|
---|
60 | set(key, value) {
|
---|
61 | const idx = asItemIndex(key);
|
---|
62 | if (typeof idx !== 'number')
|
---|
63 | throw new Error(`Expected a valid index, not ${key}.`);
|
---|
64 | const prev = this.items[idx];
|
---|
65 | if (identity.isScalar(prev) && Scalar.isScalarValue(value))
|
---|
66 | prev.value = value;
|
---|
67 | else
|
---|
68 | this.items[idx] = value;
|
---|
69 | }
|
---|
70 | toJSON(_, ctx) {
|
---|
71 | const seq = [];
|
---|
72 | if (ctx?.onCreate)
|
---|
73 | ctx.onCreate(seq);
|
---|
74 | let i = 0;
|
---|
75 | for (const item of this.items)
|
---|
76 | seq.push(toJS.toJS(item, String(i++), ctx));
|
---|
77 | return seq;
|
---|
78 | }
|
---|
79 | toString(ctx, onComment, onChompKeep) {
|
---|
80 | if (!ctx)
|
---|
81 | return JSON.stringify(this);
|
---|
82 | return stringifyCollection.stringifyCollection(this, ctx, {
|
---|
83 | blockItemPrefix: '- ',
|
---|
84 | flowChars: { start: '[', end: ']' },
|
---|
85 | itemIndent: (ctx.indent || '') + ' ',
|
---|
86 | onChompKeep,
|
---|
87 | onComment
|
---|
88 | });
|
---|
89 | }
|
---|
90 | static from(schema, obj, ctx) {
|
---|
91 | const { replacer } = ctx;
|
---|
92 | const seq = new this(schema);
|
---|
93 | if (obj && Symbol.iterator in Object(obj)) {
|
---|
94 | let i = 0;
|
---|
95 | for (let it of obj) {
|
---|
96 | if (typeof replacer === 'function') {
|
---|
97 | const key = obj instanceof Set ? it : String(i++);
|
---|
98 | it = replacer.call(obj, key, it);
|
---|
99 | }
|
---|
100 | seq.items.push(createNode.createNode(it, undefined, ctx));
|
---|
101 | }
|
---|
102 | }
|
---|
103 | return seq;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | function asItemIndex(key) {
|
---|
107 | let idx = identity.isScalar(key) ? key.value : key;
|
---|
108 | if (idx && typeof idx === 'string')
|
---|
109 | idx = Number(idx);
|
---|
110 | return typeof idx === 'number' && Number.isInteger(idx) && idx >= 0
|
---|
111 | ? idx
|
---|
112 | : null;
|
---|
113 | }
|
---|
114 |
|
---|
115 | exports.YAMLSeq = YAMLSeq;
|
---|