UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

123 lines (122 loc) 3.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setProperty = exports.isDefined = exports.isObject = exports.isFunction = exports.isNumber = exports.isString = exports.isArray = exports.filterNullValues = exports.trimString = exports.stringArrayToMapping = void 0; /** * Transform an array of strings into an object, optionally * transforming values of keys and values. * * @example * Input: ['VARIABLE', 'VAR'], k => '_' + k * Output: { _VARIABLE: 'VARIABLE', _VAR: 'VAR' } * * @param array Function that returns string. * @param transformKey Function that transforms the object key. * @param transformValue Function that transforms the object value. */ function stringArrayToMapping(array = [], transformKey = (k) => k, transformValue = (v) => v) { return array.reduce((obj, elem) => { obj[transformKey(elem)] = transformValue(elem); return obj; }, {}); } exports.stringArrayToMapping = stringArrayToMapping; /** * Trim ends of string from chars given. * Default chars are whitespaces and tabs. * * @param string String to be trimmed. * @param additional Additional character list to trim. * @param chars Defult character list to trim. */ function trimString(string, additional = '', chars = '\\s\\0\\x0B') { return string.replace(new RegExp(`^[${additional + chars}]*|[${additional + chars}]*$`, 'gim'), ''); } exports.trimString = trimString; /** * Return the same instance of object without * the properties containing null values. * * @param obj Object to be filtered. */ function filterNullValues(obj) { Object.getOwnPropertyNames(obj).forEach((name) => { if (obj[name] === null) { delete obj[name]; } }); return obj; } exports.filterNullValues = filterNullValues; /** * Test whether given value is array. * * @param value Value to be tested. */ function isArray(value) { return Array.isArray(value); } exports.isArray = isArray; /** * Test whether given value is string. * * @param value Value to be tested. */ function isString(value) { return typeof value === 'string'; } exports.isString = isString; /** * Test whether given value is number. * * @param value Value to be tested. */ function isNumber(value) { return typeof value === 'number'; } exports.isNumber = isNumber; /** * Test whether given value is function. * * @param value Value to be tested. */ function isFunction(value) { return typeof value === 'function'; } exports.isFunction = isFunction; /** * Test whether given value is object. * * @param value Value to be tested. */ function isObject(value) { return value !== null && typeof value === 'object'; } exports.isObject = isObject; /** * Test whether given value is defined and not null. * * @param value Value to be tested. */ function isDefined(value) { return typeof value !== 'undefined' && !(value === null); } exports.isDefined = isDefined; /** * Set an enumarable, configurable and writable property of given target object. * * @param target Target object to have property set on. * @param key Property key. * @param value Property value. */ function setProperty(target, key, value) { if (typeof value === 'function') { return; } Object.defineProperty(target, key, { value, enumerable: true, configurable: true, writable: true, }); } exports.setProperty = setProperty;