UNPKG

@itrocks/core-transformers

Version:

Prefabricated HTML and SQL data transformers for it.rocks primitives and basic types

145 lines 6.31 kB
import { precisionOf } from '@itrocks/precision'; import { setPropertyTypeTransformers } from '@itrocks/transformer'; import { EDIT, HTML, INPUT, OUTPUT } from '@itrocks/transformer'; import { READ, SAVE, SQL } from '@itrocks/transformer'; const lfTab = '\n\t\t\t\t'; const depends = { displayOf: (_object, property) => property, formatDate: date => date.toString(), fieldIdOf: property => property, fieldNameOf: property => property, parseDate: date => new Date(date), tr: text => text }; // Bigint export function initBigintHtmlTransformers() { setPropertyTypeTransformers(BigInt, [ { format: HTML, direction: INPUT, transformer: (value) => BigInt(value) } ]); } // Boolean function booleanEdit(value, type, property) { const fieldId = depends.fieldIdOf(property); const fieldName = depends.fieldNameOf(property); const label = `<label for="${fieldId}">${depends.tr(depends.displayOf(type, property))}</label>`; const name = `id="${fieldId}" name="${fieldName}"`; const hidden = `<input name="${fieldName}" type="hidden" value="0">`; const checked = value ? 'checked ' : ''; const checkbox = `<input ${checked}${name} type="checkbox" value="1">`; return label + lfTab + hidden + lfTab + checkbox; } const booleanInput = (value) => !['', '0', 'false', 'no', depends.tr('false'), depends.tr('no')].includes(value); export function initBooleanHtmlTransformers() { setPropertyTypeTransformers(Boolean, [ { format: HTML, direction: EDIT, transformer: booleanEdit }, { format: HTML, direction: INPUT, transformer: booleanInput }, { format: HTML, direction: OUTPUT, transformer: (value) => value ? depends.tr('yes') : depends.tr('no') } ]); } export function initBooleanSqlTransformers() { setPropertyTypeTransformers(Boolean, [ { format: SQL, direction: READ, transformer: (value) => !!value }, { format: SQL, direction: SAVE, transformer: (value) => +value } ]); } // Date function dateEdit(value, type, property) { const fieldId = depends.fieldIdOf(property); const fieldName = depends.fieldNameOf(property); const label = `<label for="${fieldId}">${depends.tr(depends.displayOf(type, property))}</label>`; const name = `id="${fieldId}" name="${fieldName}"`; const inputValue = value ? ` value="${depends.formatDate(value)}"` : ''; const input = `<input data-type="date" ${name}${inputValue}>`; return label + lfTab + input; } const dateInput = (value) => depends.parseDate(value); const dateOutput = (value) => value ? depends.formatDate(value) : ''; export function initDateHtmlTransformers() { setPropertyTypeTransformers(Date, [ { format: HTML, direction: EDIT, transformer: dateEdit }, { format: HTML, direction: INPUT, transformer: dateInput }, { format: HTML, direction: OUTPUT, transformer: dateOutput } ]); } // Number function numberEdit(value, type, property) { const output = numberOutput(value, type, property); const fieldId = depends.fieldIdOf(property); const fieldName = depends.fieldNameOf(property); const label = `<label for="${fieldId}">${depends.tr(depends.displayOf(type, property))}</label>`; const name = `id="${fieldId}" name="${fieldName}"`; const inputValue = (output !== undefined) ? ` value="${output}"` : ''; const input = `<input data-type="number" ${name}${inputValue}>`; return label + lfTab + input; } function numberInput(value) { const number = (value === '') ? undefined : +(value.replace(/\s/g, '').replace(',', '.')); if (!Number.isNaN(number)) return number; const endChar = value[value.length - 1].toUpperCase(); if (endChar === 'K') return (+value.slice(0, -1)) * 1e3; if (endChar === 'M') return (+value.slice(0, -1)) * 1e6; if (endChar === 'G') return (+value.slice(0, -2)) * 1e9; if (endChar === 'T') return (+value.slice(0, -2)) * 1e12; if (endChar === 'P') return (+value.slice(0, -2)) * 1e15; if ((endChar === 'D') && (value[value.length - 2] === 'M')) return (+value.slice(0, -2)) * 10 ** 9; return number; } function numberOutput(value, object, property) { if (value === undefined) return ''; const precision = precisionOf(object, property); return value.toLocaleString('fr-FR', { minimumFractionDigits: precision.minimum, maximumFractionDigits: precision.maximum }); } function numberRead(value) { return (typeof value === 'string') ? +value : (value ?? undefined); } export function initNumberHtmlTransformers() { setPropertyTypeTransformers(Number, [ { format: HTML, direction: EDIT, transformer: numberEdit }, { format: HTML, direction: INPUT, transformer: numberInput }, { format: HTML, direction: OUTPUT, transformer: numberOutput }, { format: SQL, direction: READ, transformer: numberRead } ]); } // default function defaultEdit(value, type, property) { const fieldId = depends.fieldIdOf(property); const fieldName = depends.fieldNameOf(property); const label = `<label for="${fieldId}">${depends.tr(depends.displayOf(type, property))}</label>`; const name = `id="${fieldId}" name="${fieldName}"`; const inputValue = value ? ` value="${value}"` : ''; const input = `<input ${name}${inputValue}>`; return label + lfTab + input; } export function initDefaultHtmlEditTransformers() { setPropertyTypeTransformers(null, [{ format: HTML, direction: EDIT, transformer: defaultEdit }]); } // all export function initPrimitiveTransformers(dependencies = {}) { setPrimitiveDependencies(dependencies); initBigintHtmlTransformers(); initBooleanSqlTransformers(); initBooleanHtmlTransformers(); initDateHtmlTransformers(); initNumberHtmlTransformers(); initDefaultHtmlEditTransformers(); } // Initialize dependencies required by all core primitive transformers except Date export function setCorePrimitiveDependencies(dependencies) { Object.assign(depends, dependencies); } // Initialize dependencies required by all core primitive transformers, including Date export function setPrimitiveDependencies(dependencies) { Object.assign(depends, dependencies); } //# sourceMappingURL=primitive.js.map