123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- const {InnerState} = require('../inner-state');
- const {assert} = require('../assert');
- const npm = {
- os: require('os'),
- utils: require('../utils'),
- formatting: require('../formatting'),
- patterns: require('../patterns')
- };
- class Column extends InnerState {
- constructor(col) {
- super();
- if (typeof col === 'string') {
- const info = parseColumn(col);
- this.name = info.name;
- if ('mod' in info) {
- this.mod = info.mod;
- }
- if ('cnd' in info) {
- this.cnd = info.cnd;
- }
- } else {
- col = assert(col, ['name', 'prop', 'mod', 'cast', 'cnd', 'def', 'init', 'skip']);
- if ('name' in col) {
- if (!npm.utils.isText(col.name)) {
- throw new TypeError(`Invalid 'name' value: ${npm.utils.toJson(col.name)}. A non-empty string was expected.`);
- }
- if (npm.utils.isNull(col.prop) && !isValidVariable(col.name)) {
- throw new TypeError(`Invalid 'name' syntax: ${npm.utils.toJson(col.name)}.`);
- }
- this.name = col.name;
- if (!npm.utils.isNull(col.prop)) {
- if (!npm.utils.isText(col.prop)) {
- throw new TypeError(`Invalid 'prop' value: ${npm.utils.toJson(col.prop)}. A non-empty string was expected.`);
- }
- if (!isValidVariable(col.prop)) {
- throw new TypeError(`Invalid 'prop' syntax: ${npm.utils.toJson(col.prop)}.`);
- }
- if (col.prop !== col.name) {
-
- this.prop = col.prop;
- }
- }
- if (!npm.utils.isNull(col.mod)) {
- if (typeof col.mod !== 'string' || !isValidMod(col.mod)) {
- throw new TypeError(`Invalid 'mod' value: ${npm.utils.toJson(col.mod)}.`);
- }
- this.mod = col.mod;
- }
- if (!npm.utils.isNull(col.cast)) {
- this.cast = parseCast(col.cast);
- }
- if ('cnd' in col) {
- this.cnd = !!col.cnd;
- }
- if ('def' in col) {
- this.def = col.def;
- }
- if (typeof col.init === 'function') {
- this.init = col.init;
- }
- if (typeof col.skip === 'function') {
- this.skip = col.skip;
- }
- } else {
- throw new TypeError('Invalid column details.');
- }
- }
- const variable = '${' + (this.prop || this.name) + (this.mod || '') + '}';
- const castText = this.cast ? ('::' + this.cast) : '';
- const escapedName = npm.formatting.as.name(this.name);
- this.extendState({variable, castText, escapedName});
- Object.freeze(this);
- }
-
- get variable() {
- return this._inner.variable;
- }
-
- get castText() {
- return this._inner.castText;
- }
-
- get escapedName() {
- return this._inner.escapedName;
- }
- }
- function parseCast(name) {
- if (typeof name === 'string') {
- const s = name.replace(/^[:\s]*|\s*$/g, '');
- if (s) {
- return s;
- }
- }
- throw new TypeError(`Invalid 'cast' value: ${npm.utils.toJson(name)}.`);
- }
- function parseColumn(name) {
- const m = name.match(npm.patterns.validColumn);
- if (m && m[0] === name) {
- const res = {};
- if (name[0] === '?') {
- res.cnd = true;
- name = name.substring(1);
- }
- const mod = name.match(npm.patterns.hasValidModifier);
- if (mod) {
- res.name = name.substring(0, mod.index);
- res.mod = mod[0];
- } else {
- res.name = name;
- }
- return res;
- }
- throw new TypeError(`Invalid column syntax: ${npm.utils.toJson(name)}.`);
- }
- function isValidMod(mod) {
- return npm.patterns.validModifiers.indexOf(mod) !== -1;
- }
- function isValidVariable(name) {
- const m = name.match(npm.patterns.validVariable);
- return !!m && m[0] === name;
- }
- Column.prototype.toString = function (level) {
- level = level > 0 ? parseInt(level) : 0;
- const gap0 = npm.utils.messageGap(level),
- gap1 = npm.utils.messageGap(level + 1),
- lines = [
- gap0 + 'Column {',
- gap1 + 'name: ' + npm.utils.toJson(this.name)
- ];
- if ('prop' in this) {
- lines.push(gap1 + 'prop: ' + npm.utils.toJson(this.prop));
- }
- if ('mod' in this) {
- lines.push(gap1 + 'mod: ' + npm.utils.toJson(this.mod));
- }
- if ('cast' in this) {
- lines.push(gap1 + 'cast: ' + npm.utils.toJson(this.cast));
- }
- if ('cnd' in this) {
- lines.push(gap1 + 'cnd: ' + npm.utils.toJson(this.cnd));
- }
- if ('def' in this) {
- lines.push(gap1 + 'def: ' + npm.utils.toJson(this.def));
- }
- if ('init' in this) {
- lines.push(gap1 + 'init: [Function]');
- }
- if ('skip' in this) {
- lines.push(gap1 + 'skip: [Function]');
- }
- lines.push(gap0 + '}');
- return lines.join(npm.os.EOL);
- };
- npm.utils.addInspection(Column, function () {
- return this.toString();
- });
- module.exports = {Column};
|