@contrail/types
Advanced tools
Comparing version 2.0.46 to 2.0.47
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,5 +14,5 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./property-value-formatter"), exports); |
@@ -17,3 +17,4 @@ import { DateFormatingOptions, NumberFormatingOptions, TypeProperty } from '../type-properties'; | ||
formatCurrencyValue(value: any): string; | ||
formatDate(isoString: string | null, dateFormatOptions?: DateFormatingOptions): string; | ||
formatDate(date: any, dateFormatOptions?: DateFormatingOptions): string; | ||
formatDateTime(date: any, dateFormatOptions?: DateFormatingOptions): string; | ||
formatBoolean(value: boolean): string; | ||
@@ -20,0 +21,0 @@ formatObjectReference(object: any): any; |
@@ -27,3 +27,3 @@ "use strict"; | ||
if (!value || !property) { | ||
if ((property === null || property === void 0 ? void 0 : property.propertyType) === 'boolean') { | ||
if (property?.propertyType === 'boolean') { | ||
return this.formatBoolean(value); | ||
@@ -133,9 +133,39 @@ } | ||
} | ||
formatDate(isoString, dateFormatOptions = { includeTime: false }) { | ||
if (!isoString) { | ||
formatDate(date, dateFormatOptions = { includeTime: false }) { | ||
if (dateFormatOptions?.includeTime) { | ||
return this.formatDateTime(date, dateFormatOptions); | ||
} | ||
if (!date) { | ||
return ''; | ||
} | ||
const options = {}; | ||
return new Intl.DateTimeFormat(this.localizationConfig.locale, options).format(new Date(isoString).getTime()); | ||
if (typeof date === 'object') { | ||
date = '' + date; | ||
} | ||
if (date.charAt(date.length - 1).toUpperCase() === 'Z') { | ||
date = date.substring(0, date.length - 1); | ||
} | ||
const options = { | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
}; | ||
return new Intl.DateTimeFormat(this.localizationConfig.locale, options).format(new Date(date).getTime()); | ||
} | ||
formatDateTime(date, dateFormatOptions = { includeTime: false }) { | ||
if (!date) { | ||
return ''; | ||
} | ||
if (typeof date === 'object') { | ||
date = '' + date; | ||
} | ||
const options = { | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
year: 'numeric', | ||
month: '2-digit', | ||
day: '2-digit', | ||
}; | ||
return new Intl.DateTimeFormat(this.localizationConfig.locale, options).format(new Date(date).getTime()); | ||
} | ||
formatBoolean(value) { | ||
@@ -150,3 +180,3 @@ if (value) { | ||
formatObjectReference(object) { | ||
const objectName = (object === null || object === void 0 ? void 0 : object.optionName) || (object === null || object === void 0 ? void 0 : object.name) || ''; | ||
const objectName = object?.optionName || object?.name || ''; | ||
return objectName; | ||
@@ -153,0 +183,0 @@ } |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FormulaFunctionProcessor = void 0; | ||
class FormulaFunctionProcessor { | ||
static processFormulaFunctionsForEntity(entity, properties, context = {}) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
for (const property of properties) { | ||
if ((_a = property.formulaFunction) === null || _a === void 0 ? void 0 : _a.length) { | ||
const value = yield this.processFormulaFunction(property.formulaFunction, entity, context); | ||
if (value) { | ||
entity[property.slug] = value; | ||
} | ||
static async processFormulaFunctionsForEntity(entity, properties, context = {}) { | ||
for (const property of properties) { | ||
if (property.formulaFunction?.length) { | ||
const value = await this.processFormulaFunction(property.formulaFunction, entity, context); | ||
if (value) { | ||
entity[property.slug] = value; | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
static processFormulaFunction(formulaFunctionString = '', data, context = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let value; | ||
try { | ||
const formulaFunction = this.getFormulaFunction(formulaFunctionString); | ||
value = yield formulaFunction(data, context); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
console.error('FormulaFunctionProcessor: error processing function: ', formulaFunctionString); | ||
} | ||
return value; | ||
}); | ||
static async processFormulaFunction(formulaFunctionString = '', data, context = {}) { | ||
let value; | ||
try { | ||
const formulaFunction = this.getFormulaFunction(formulaFunctionString); | ||
value = await formulaFunction(data, context); | ||
} | ||
catch (e) { | ||
console.error(e); | ||
console.error('FormulaFunctionProcessor: error processing function: ', formulaFunctionString); | ||
} | ||
return value; | ||
} | ||
@@ -48,3 +34,3 @@ static processFormulasForEntities(entities, properties) { | ||
try { | ||
const AsyncFunction = Object.getPrototypeOf(() => __awaiter(this, void 0, void 0, function* () { })).constructor; | ||
const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor; | ||
const executionFunction = new AsyncFunction('obj', 'context', functionString); | ||
@@ -51,0 +37,0 @@ return executionFunction; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,3 +14,3 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
@@ -13,0 +17,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,3 +14,3 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
@@ -13,0 +17,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,5 +14,5 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./type-interface"), exports); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,3 +14,3 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
@@ -13,0 +17,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,5 +14,5 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./type-root"), exports); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,5 +14,5 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./type"), exports); |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
@@ -10,3 +14,3 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
@@ -13,0 +17,0 @@ Object.defineProperty(exports, "__esModule", { value: true }); |
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -21,54 +12,48 @@ exports.TypeManagedValidator = exports.PROPERTY_IS_NOT_EDITABLE = exports.VALUE_IS_NOT_A_VALID_MULTI_SELECT = exports.VALUE_IS_NOT_A_VALID_BOOLEAN = exports.VALUE_IS_NOT_A_VALID_DATE = exports.VALUE_IS_NOT_A_VALID_NUMBER = exports.VALUE_IS_NOT_A_VALID_OPTION = void 0; | ||
class TypeManagedValidator { | ||
static validateEntity(entity, type, changes = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const validationErrors = {}; | ||
let validationErrorCount = 0; | ||
const typeProperties = type.typeProperties || []; | ||
for (const prop of typeProperties) { | ||
const errors = yield this.validateProperty(entity, prop); | ||
if (changes[prop.slug] && !prop.editable) { | ||
errors.push({ value: changes[prop.slug], message: exports.PROPERTY_IS_NOT_EDITABLE }); | ||
} | ||
if (errors === null || errors === void 0 ? void 0 : errors.length) { | ||
validationErrorCount += errors.length; | ||
validationErrors[prop.slug] = errors; | ||
} | ||
static async validateEntity(entity, type, changes = {}) { | ||
const validationErrors = {}; | ||
let validationErrorCount = 0; | ||
const typeProperties = type.typeProperties || []; | ||
for (const prop of typeProperties) { | ||
const errors = await this.validateProperty(entity, prop); | ||
if (changes[prop.slug] && !prop.editable) { | ||
errors.push({ value: changes[prop.slug], message: exports.PROPERTY_IS_NOT_EDITABLE }); | ||
} | ||
return { validationErrorCount, validationErrors }; | ||
}); | ||
if (errors?.length) { | ||
validationErrorCount += errors.length; | ||
validationErrors[prop.slug] = errors; | ||
} | ||
} | ||
return { validationErrorCount, validationErrors }; | ||
} | ||
static validateProperty(entity, property) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const value = entity[property.slug || '']; | ||
return this.validatePropertyValue(value, property); | ||
}); | ||
static async validateProperty(entity, property) { | ||
const value = entity[property.slug || '']; | ||
return this.validatePropertyValue(value, property); | ||
} | ||
static validatePropertyValue(value, property) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let validationErrors = []; | ||
if (!value) { | ||
return []; | ||
} | ||
switch (property.propertyType) { | ||
case __1.PropertyType.Date: | ||
validationErrors = this.validateDate(value); | ||
break; | ||
case __1.PropertyType.Currency: | ||
case __1.PropertyType.Sequence: | ||
case __1.PropertyType.Percent: | ||
case __1.PropertyType.Number: | ||
validationErrors = this.validateNumber(value); | ||
break; | ||
case __1.PropertyType.SingleSelect: | ||
validationErrors = this.validateOptionValue(value, property); | ||
break; | ||
case __1.PropertyType.MultiSelect: | ||
validationErrors = this.validateMultiOptionValue(value, property); | ||
break; | ||
case __1.PropertyType.Boolean: | ||
validationErrors = this.validateBoolean(value, property); | ||
break; | ||
} | ||
return validationErrors; | ||
}); | ||
static async validatePropertyValue(value, property) { | ||
let validationErrors = []; | ||
if (!value) { | ||
return []; | ||
} | ||
switch (property.propertyType) { | ||
case __1.PropertyType.Date: | ||
validationErrors = this.validateDate(value); | ||
break; | ||
case __1.PropertyType.Currency: | ||
case __1.PropertyType.Sequence: | ||
case __1.PropertyType.Percent: | ||
case __1.PropertyType.Number: | ||
validationErrors = this.validateNumber(value); | ||
break; | ||
case __1.PropertyType.SingleSelect: | ||
validationErrors = this.validateOptionValue(value, property); | ||
break; | ||
case __1.PropertyType.MultiSelect: | ||
validationErrors = this.validateMultiOptionValue(value, property); | ||
break; | ||
case __1.PropertyType.Boolean: | ||
validationErrors = this.validateBoolean(value, property); | ||
break; | ||
} | ||
return validationErrors; | ||
} | ||
@@ -98,5 +83,4 @@ static validateNumber(value) { | ||
static validateOptionValue(value, property) { | ||
var _a; | ||
const errors = []; | ||
const option = (_a = property.options) === null || _a === void 0 ? void 0 : _a.find(opt => opt.value === value); | ||
const option = property.options?.find(opt => opt.value === value); | ||
if (!option && true) { | ||
@@ -108,3 +92,2 @@ errors.push({ value, message: exports.VALUE_IS_NOT_A_VALID_OPTION }); | ||
static validateMultiOptionValue(values, property) { | ||
var _a; | ||
const errors = []; | ||
@@ -122,3 +105,3 @@ if (!values) { | ||
for (const value of values) { | ||
const option = (_a = property.options) === null || _a === void 0 ? void 0 : _a.find(opt => opt.value === value); | ||
const option = property.options?.find(opt => opt.value === value); | ||
if (!option && true) { | ||
@@ -125,0 +108,0 @@ errors.push({ value, message: exports.VALUE_IS_NOT_A_VALID_OPTION }); |
{ | ||
"name": "@contrail/types", | ||
"version": "2.0.46", | ||
"version": "2.0.47", | ||
"description": "Types Utility module", | ||
@@ -17,2 +17,3 @@ "main": "lib/index.js", | ||
"@types/jest": "^23.3.14", | ||
"@types/node": "^14.18.12", | ||
"jest": "^23.6.0", | ||
@@ -23,4 +24,3 @@ "prettier": "^1.19.1", | ||
"tslint-config-prettier": "^1.18.0", | ||
"typescript": "^3.0.1", | ||
"@types/node": "^14.6.4" | ||
"typescript": "^4.6.3" | ||
}, | ||
@@ -27,0 +27,0 @@ "jest": { |
38649
909