@react_db_client/helpers.data-processing
Advanced tools
Comparing version 0.2.23 to 0.2.24
@@ -1,1 +0,10 @@ | ||
export function validateValue(vIn: number, min: number, max: number, step: number, defaultValue: number): number | ""; | ||
/** | ||
* Validate and sanitize a number value | ||
* @param {number} vIn value to validate | ||
* @param {number} min minium allowed value | ||
* @param {number} max max allowed value | ||
* @param {number} step decimal number identifying number of decimal places | ||
* @param {number} defaultValue The default value to use if is invalid | ||
* @returns number | ||
*/ | ||
export declare const validateValue: (vIn: number | null, min: number, max: number, step: number, defaultValue?: number) => number | ''; |
@@ -1,2 +0,2 @@ | ||
export { validateValue } from "./data-validation"; | ||
export { formatValue, getDefaultValue, sanitizeNumber, sanitizeCellData, stringifyNumber, stringifyText, stringifyBool, stringifyDict, stringifyDate, stringifyImage, stringifyData } from "./stringify-data"; | ||
export { formatValue, getDefaultValue, sanitizeNumber, sanitizeCellData, stringifyNumber, stringifyText, stringifyBool, stringifyDict, stringifyDate, stringifyImage, stringifyData, } from './stringify-data'; | ||
export { validateValue, } from './data-validation'; |
@@ -1,11 +0,64 @@ | ||
export function formatValue(v: number, step: number, strict: any): number; | ||
export function getDefaultValue(data: object): any; | ||
export function sanitizeNumber(cellData: any, columnData?: {}): number | "Invalid"; | ||
export function sanitizeCellData(cellData: any, columnData?: {}): any; | ||
export function stringifyNumber(data: any, metaData: any): string; | ||
export function stringifyText(data: any, _metaData: any): any; | ||
export function stringifyBool(data: any, _metaData: any): "Yes" | "No"; | ||
export function stringifyDict(data: any, metaData: any): any; | ||
export function stringifyDate(data: any, _metaData: any): any; | ||
export function stringifyImage(data: any, _metaData: any): any; | ||
export function stringifyData(item: any, metaData: object, customParsers?: any, strict?: boolean): any; | ||
import { EFilterType, Uid } from '@react_db_client/constants.client-types'; | ||
/** | ||
* Format a number with decimal place | ||
* @param {number} v The number to format | ||
* @param {number} step decimal number to limit decimal places | ||
* @returns number | ||
*/ | ||
export declare const formatValue: (v: number, step?: number, strict?: boolean) => number; | ||
/** | ||
* | ||
* @param {object} data data with default values and type | ||
* @returns | ||
*/ | ||
export declare const getDefaultValue: <T extends unknown>(data: { | ||
defaultValue?: T; | ||
type?: EFilterType; | ||
}) => "" | T; | ||
export declare const sanitizeNumber: (cellData: any, columnData?: { | ||
step?: number; | ||
}) => number | 'Invalid'; | ||
export declare const sanitizeCellData: <T>(cellData: any, columnData?: { | ||
defaultValue?: T; | ||
label?: string; | ||
name?: string; | ||
type?: EFilterType; | ||
step?: number; | ||
}) => unknown; | ||
export declare const stringifyNumber: (data: string | number, metaData: { | ||
step?: number; | ||
commas?: boolean; | ||
}) => string; | ||
export declare const stringifyText: (data: null | string, _metaData: any) => string; | ||
export declare const stringifyBool: (data: boolean, _metaData: any) => "Yes" | "No"; | ||
export declare const stringifyDict: (data: { | ||
label?: string; | ||
name?: string; | ||
}, metaData: { | ||
labelField?: string; | ||
}) => any; | ||
export declare const stringifyDate: (data: Date, _metaData: any) => any; | ||
export declare const stringifyImage: (data: string, _metaData: any) => string; | ||
export declare type Parser = (data: any, metadata: {}) => string; | ||
/** | ||
* Parse an item using meta data and type based parser | ||
* | ||
* Meta data contents: | ||
* - type | ||
* - step | ||
* | ||
* Parser signature | ||
* (data, metaData) => parsedValue | ||
* | ||
* @param {any} item the data to parse | ||
* @param {object} metaData column data to parse to | ||
* @param {objectOf} customParsers dict containing custom parsers | ||
* @returns parsed item | ||
*/ | ||
export declare const stringifyData: (item: any, metaData: { | ||
uid: Uid; | ||
type: EFilterType | string; | ||
step?: number; | ||
commas?: boolean; | ||
labelField?: string; | ||
}, customParsers?: {}, strict?: boolean) => any; |
@@ -12,3 +12,3 @@ "use strict"; | ||
*/ | ||
const formatValue = (v, step = 0.01, strict) => { | ||
const formatValue = (v, step = 0.01, strict = false) => { | ||
const [whole] = v.toFixed(100).split('.'); | ||
@@ -26,3 +26,3 @@ if (!Number(v) && !strict) | ||
if (vLength >= step.toString().length) | ||
return Math.round(whole / step) * step; | ||
return Math.round(Number(whole) / step) * step; | ||
const vSuff = v | ||
@@ -47,7 +47,7 @@ .toString() | ||
return data.defaultValue; | ||
if (data.type === constants_client_types_1.filterTypes.number) | ||
if (data.type === constants_client_types_1.EFilterType.number) | ||
return 0; | ||
if (data.type === constants_client_types_1.filterTypes.bool) | ||
if (data.type === constants_client_types_1.EFilterType.bool) | ||
return false; | ||
if (data.type === constants_client_types_1.filterTypes.toggle) | ||
if (data.type === constants_client_types_1.EFilterType.toggle) | ||
return false; | ||
@@ -68,2 +68,3 @@ return ''; | ||
const stringifyNumber = (data, metaData) => { | ||
var _a; | ||
const { step, commas } = metaData; | ||
@@ -86,10 +87,7 @@ if (data === null || data === undefined) | ||
// TODO: Need to reverse | ||
const wholeCommas = whole | ||
const wholeCommas = ((_a = whole | ||
.split('') | ||
.reverse() | ||
.join('') | ||
.match(/.{1,3}/g) | ||
.map((v) => v.split('').reverse().join('')) | ||
.reverse() | ||
.join(','); | ||
.match(/.{1,3}/g)) === null || _a === void 0 ? void 0 : _a.map((v) => v.split('').reverse().join('')).reverse().join(',')) || 'Invalid'; | ||
return `${wholeCommas}.${formattedPoint}`; | ||
@@ -100,3 +98,3 @@ } | ||
exports.stringifyNumber = stringifyNumber; | ||
const stringifyText = (data, _metaData) => (data === null || data === undefined ? '' : data); | ||
const stringifyText = (data, _metaData) => data === null || data === undefined ? '' : data; | ||
exports.stringifyText = stringifyText; | ||
@@ -137,3 +135,3 @@ const stringifyBool = (data, _metaData) => (data ? 'Yes' : 'No'); | ||
const stringifyData = (item, metaData, customParsers = {}, strict = true) => { | ||
const parser = (0, helpers_func_tools_1.switchF)(metaData.type, Object.assign({ text: () => exports.stringifyText, textLong: () => exports.stringifyText, button: () => exports.stringifyText, select: () => exports.stringifyText, link: () => exports.stringifyText, number: () => exports.stringifyNumber, bool: () => exports.stringifyBool, toggle: () => exports.stringifyBool, dict: () => exports.stringifyDict, date: () => exports.stringifyDate, image: () => exports.stringifyImage, reference: () => exports.stringifyDict }, Object.entries(customParsers).reduce((acc, [key, customParser]) => { | ||
const parser = (0, helpers_func_tools_1.switchF)(metaData.type, Object.assign({ [constants_client_types_1.EFilterType.text]: () => exports.stringifyText, [constants_client_types_1.EFilterType.textLong]: () => exports.stringifyText, [constants_client_types_1.EFilterType.button]: () => exports.stringifyText, [constants_client_types_1.EFilterType.select]: () => exports.stringifyText, [constants_client_types_1.EFilterType.number]: () => exports.stringifyNumber, [constants_client_types_1.EFilterType.bool]: () => exports.stringifyBool, [constants_client_types_1.EFilterType.toggle]: () => exports.stringifyBool, [constants_client_types_1.EFilterType.dict]: () => exports.stringifyDict, [constants_client_types_1.EFilterType.date]: () => exports.stringifyDate, [constants_client_types_1.EFilterType.image]: () => exports.stringifyImage, [constants_client_types_1.EFilterType.reference]: () => exports.stringifyDict, link: () => exports.stringifyText }, Object.entries(customParsers).reduce((acc, [key, customParser]) => { | ||
acc[key] = () => customParser; | ||
@@ -140,0 +138,0 @@ return acc; |
@@ -18,10 +18,10 @@ "use strict"; | ||
const options = [ | ||
['hello', constants_client_types_1.filterTypes.text, 'hello'], | ||
['hello', constants_client_types_1.filterTypes.number, 'Invalid', { step: 0.1 }], | ||
['43.1234', constants_client_types_1.filterTypes.number, 43.1, { step: 0.1 }], | ||
[undefined, constants_client_types_1.filterTypes.number, 0, { step: 0.1 }], | ||
[null, constants_client_types_1.filterTypes.number, 0, { step: 0.1 }], | ||
[undefined, constants_client_types_1.filterTypes.string, ''], | ||
[null, constants_client_types_1.filterTypes.string, ''], | ||
[12345, constants_client_types_1.filterTypes.number, 12345.0, { step: 0.1 }], | ||
['hello', constants_client_types_1.EFilterType.text, 'hello'], | ||
['hello', constants_client_types_1.EFilterType.number, 'Invalid', { step: 0.1 }], | ||
['43.1234', constants_client_types_1.EFilterType.number, 43.1, { step: 0.1 }], | ||
[undefined, constants_client_types_1.EFilterType.number, 0, { step: 0.1 }], | ||
[null, constants_client_types_1.EFilterType.number, 0, { step: 0.1 }], | ||
[undefined, constants_client_types_1.EFilterType.text, ''], | ||
[null, constants_client_types_1.EFilterType.text, ''], | ||
[12345, constants_client_types_1.EFilterType.number, 12345.0, { step: 0.1 }], | ||
]; | ||
@@ -41,20 +41,20 @@ options.forEach(([v, type, out, args]) => { | ||
const options = [ | ||
['hello', { type: constants_client_types_1.filterTypes.text }, 'hello'], | ||
['hello', { type: constants_client_types_1.filterTypes.number, step: 0.1 }, 'Invalid'], | ||
['43.1234', { type: constants_client_types_1.filterTypes.number, step: 0.1 }, '43.1'], | ||
[43.1234, { type: constants_client_types_1.filterTypes.number, step: 0.1 }, '43.1'], | ||
[43.1234, { type: constants_client_types_1.filterTypes.number, step: 0.01 }, '43.12'], | ||
[43.1234, { type: constants_client_types_1.filterTypes.number, step: 1 }, '43'], | ||
[43.1234, { type: constants_client_types_1.filterTypes.number, step: 10 }, '40'], | ||
[43, { type: constants_client_types_1.filterTypes.number, step: 0.01 }, '43.00'], | ||
[11222333, { type: constants_client_types_1.filterTypes.number, step: 0.01, commas: true }, '11,222,333.00'], | ||
[12345678.9, { type: constants_client_types_1.filterTypes.number, step: 0.01, commas: true }, '12,345,678.90'], | ||
[12345678.9, { type: constants_client_types_1.filterTypes.number, step: 0.01, commas: true }, '12,345,678.90'], | ||
[123, { type: constants_client_types_1.filterTypes.number, step: 0.01, commas: true }, '123.00'], | ||
[123, { type: constants_client_types_1.filterTypes.number, step: 1, commas: true }, '123'], | ||
[undefined, { type: constants_client_types_1.filterTypes.number, step: 0.1 }, ''], | ||
[null, { type: constants_client_types_1.filterTypes.number, step: 0.1 }, ''], | ||
[undefined, { type: constants_client_types_1.filterTypes.text }, ''], | ||
[null, { type: constants_client_types_1.filterTypes.text }, ''], | ||
['', { type: constants_client_types_1.filterTypes.text }, ''], | ||
['hello', { type: constants_client_types_1.EFilterType.text }, 'hello'], | ||
['hello', { type: constants_client_types_1.EFilterType.number, step: 0.1 }, 'Invalid'], | ||
['43.1234', { type: constants_client_types_1.EFilterType.number, step: 0.1 }, '43.1'], | ||
[43.1234, { type: constants_client_types_1.EFilterType.number, step: 0.1 }, '43.1'], | ||
[43.1234, { type: constants_client_types_1.EFilterType.number, step: 0.01 }, '43.12'], | ||
[43.1234, { type: constants_client_types_1.EFilterType.number, step: 1 }, '43'], | ||
[43.1234, { type: constants_client_types_1.EFilterType.number, step: 10 }, '40'], | ||
[43, { type: constants_client_types_1.EFilterType.number, step: 0.01 }, '43.00'], | ||
[11222333, { type: constants_client_types_1.EFilterType.number, step: 0.01, commas: true }, '11,222,333.00'], | ||
[12345678.9, { type: constants_client_types_1.EFilterType.number, step: 0.01, commas: true }, '12,345,678.90'], | ||
[12345678.9, { type: constants_client_types_1.EFilterType.number, step: 0.01, commas: true }, '12,345,678.90'], | ||
[123, { type: constants_client_types_1.EFilterType.number, step: 0.01, commas: true }, '123.00'], | ||
[123, { type: constants_client_types_1.EFilterType.number, step: 1, commas: true }, '123'], | ||
[undefined, { type: constants_client_types_1.EFilterType.number, step: 0.1 }, ''], | ||
[null, { type: constants_client_types_1.EFilterType.number, step: 0.1 }, ''], | ||
[undefined, { type: constants_client_types_1.EFilterType.text }, ''], | ||
[null, { type: constants_client_types_1.EFilterType.text }, ''], | ||
['', { type: constants_client_types_1.EFilterType.text }, ''], | ||
]; | ||
@@ -65,3 +65,3 @@ const customParsers = []; | ||
test(`should stringify value ${v} with column type ${type}`, () => { | ||
const vout = (0, stringify_data_1.stringifyData)(v, metaData, customParsers); | ||
const vout = (0, stringify_data_1.stringifyData)(v, Object.assign({ uid: 'test' }, metaData), customParsers); | ||
expect(vout).toEqual(out); | ||
@@ -68,0 +68,0 @@ }); |
{ | ||
"name": "@react_db_client/helpers.data-processing", | ||
"version": "0.2.23", | ||
"version": "0.2.24", | ||
"homepage": "https://bit.dev/samnbuk/react_db_client/helpers/data-processing", | ||
@@ -9,3 +9,3 @@ "main": "dist/index.js", | ||
"name": "helpers/data-processing", | ||
"version": "0.2.23" | ||
"version": "0.2.24" | ||
}, | ||
@@ -12,0 +12,0 @@ "dependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
57935
828
1