UNPKG

@citrineos/util

Version:

The OCPP util module which supplies helpful utilities like cache and queue connectors, etc.

63 lines 1.91 kB
"use strict"; // Copyright Contributors to the CitrineOS Project // // SPDX-License-Identifier: Apache 2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.getSizeOfRequest = getSizeOfRequest; exports.getBatches = getBatches; exports.getNumberOfFractionDigit = getNumberOfFractionDigit; exports.stringToSet = stringToSet; /** * Calculate the size of a request. * * @param {object} request - The ocpp request. * @return {number} The size of the request (Bytes). */ function getSizeOfRequest(request) { return new TextEncoder().encode(JSON.stringify(request)).length; } /** * Slice array into pieces according to the given size. * * @param {array} array - An array. * @param {number} size - The expected size of a batch. * @return {map} A map with index as key and batch as value. Index is the position of the 1st batch element in the given * array. Batch is a subarray of the given array. */ function getBatches(array, size) { const batchMap = new Map(); let lastIndex = 0; while (array.length > 0) { const batch = array.slice(0, size); batchMap.set(lastIndex, batch); lastIndex += batch.length; array = array.slice(size); } return batchMap; } /** * Get the number of fraction digits in a number, e.g, 1.23 -> 2 * * @param num - The number to get the number of fraction digits * @returns The number of fraction digits */ function getNumberOfFractionDigit(num) { const numString = num.toString(); const match = numString.match(/\.(\d+)/); if (!match) { return 0; } else { return match[1].length; } } /** * Convert string to set. For example, 'a,b,c' -> new Set(['a', 'b', 'c']) * * @param input - The string to convert * @returns Set */ function stringToSet(input) { return new Set(input.split(',').map((item) => item.trim())); } //# sourceMappingURL=parser.js.map