Sha256: 90d2aaf12f1f52bd8df84f8585b5a927611d280e22e03a1b97a6854a0b858eb7

Contents?: true

Size: 1.95 KB

Versions: 24

Compression:

Stored size: 1.95 KB

Contents

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _toJSON = _interopRequireDefault(require("../toJSON"));

var _Collection = _interopRequireDefault(require("./Collection"));

var _Scalar = _interopRequireDefault(require("./Scalar"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

// Published as 'yaml/seq'
function asItemIndex(key) {
  let idx = key instanceof _Scalar.default ? key.value : key;
  if (idx && typeof idx === 'string') idx = Number(idx);
  return Number.isInteger(idx) && idx >= 0 ? idx : null;
}

class YAMLSeq extends _Collection.default {
  add(value) {
    this.items.push(value);
  }

  delete(key) {
    const idx = asItemIndex(key);
    if (typeof idx !== 'number') return false;
    const del = this.items.splice(idx, 1);
    return del.length > 0;
  }

  get(key, keepScalar) {
    const idx = asItemIndex(key);
    if (typeof idx !== 'number') return undefined;
    const it = this.items[idx];
    return !keepScalar && it instanceof _Scalar.default ? it.value : it;
  }

  has(key) {
    const idx = asItemIndex(key);
    return typeof idx === 'number' && idx < this.items.length;
  }

  set(key, value) {
    const idx = asItemIndex(key);
    if (typeof idx !== 'number') throw new Error(`Expected a valid index, not ${key}.`);
    this.items[idx] = value;
  }

  toJSON(_, ctx) {
    const seq = [];
    if (ctx && ctx.onCreate) ctx.onCreate(seq);
    let i = 0;

    for (const item of this.items) seq.push((0, _toJSON.default)(item, String(i++), ctx));

    return seq;
  }

  toString(ctx, onComment, onChompKeep) {
    if (!ctx) return JSON.stringify(this);
    return super.toString(ctx, {
      blockItem: n => n.type === 'comment' ? n.str : `- ${n.str}`,
      flowChars: {
        start: '[',
        end: ']'
      },
      isMap: false,
      itemIndent: (ctx.indent || '') + '  '
    }, onComment, onChompKeep);
  }

}

exports.default = YAMLSeq;

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
trusty-cms-5.0.7 node_modules/yaml/dist/schema/Seq.js
trusty-cms-5.0.6 node_modules/yaml/dist/schema/Seq.js
trusty-cms-5.0.5 node_modules/yaml/dist/schema/Seq.js
trusty-cms-5.0.4 node_modules/yaml/dist/schema/Seq.js
trusty-cms-5.0.3 node_modules/yaml/dist/schema/Seq.js
trusty-cms-5.0.2 node_modules/yaml/dist/schema/Seq.js
trusty-cms-5.0.1 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.3.5 node_modules/yaml/dist/schema/Seq.js
trusty-cms-5.0.0 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.3.4 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.3.3 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.3.2 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.3.1 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.3 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.2.3 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.2.2 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.2.1 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.2 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.1.9 node_modules/yaml/dist/schema/Seq.js
trusty-cms-4.1.8 node_modules/yaml/dist/schema/Seq.js