UNPKG

ts-utls

Version:

Utilities for TypeScript library

112 lines (108 loc) 4.1 kB
"use strict"; /* MIT License Copyright (c) 2020 Cyril Dever Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.xor = exports.toHex = exports.splitCamelCaseWords = exports.shuffle = exports.reverse = exports.hashCode = exports.fromHex = exports.capitalize = void 0; /** * Put the first letter of a sentence in upper case * * @param {string} s - The sentence to capitalize * @returns the capitalized string */ const capitalize = (s) => s.length > 0 ? s.replace(/^./, s[0].toUpperCase()) : s; exports.capitalize = capitalize; /** * Returns the byte array from the passed hexadecimal string, or an empty buffer if failed * * @param {string} s - The hexadecimal string representation * @returns the corresponding byte array */ const fromHex = (s) => { try { return Buffer.from(s, 'hex'); } catch (_) { // eslint-disable-line @typescript-eslint/no-unused-vars return Buffer.alloc(0); } }; exports.fromHex = fromHex; /** * Compute the equivalent of Java's hashCode for the passed string * * @param {string} s - The string to use * @returns the hashCode */ const hashCode = (s) => Array.from(s).reduce((h, c) => Math.imul(31, h) + c.charCodeAt(0) | 0, 0); exports.hashCode = hashCode; /** * Reverse the order of the characters in the passed string * * @param {string} s - The string to use * @returns the reversed string */ const reverse = (s) => [...s].reduce((reversed, character) => character + reversed, ''); exports.reverse = reverse; /** * Shuffle the characters of the passed string * * @param {string} s - The string to use * @returns the shuffled string * @see http://en.wikipedia.org/wiki/Fisher-Yates_shuffle */ const shuffle = (s) => Array.from(s).reverse().reduce((shuffled, c, idx) => { const j = Math.floor(Math.random() * (idx + 1)); const tmp = shuffled.split(''); tmp[idx] = shuffled[j]; tmp[j] = c; return tmp.join(''); }); exports.shuffle = shuffle; /** * Split a camel case string into a sequence of words, eg. MyCamelCaseWord => My Camel Case Word * * @param {string} s - The camel case string to use * @returns the sentence */ const splitCamelCaseWords = (s) => s.replace(/([a-z0-9])([A-Z])/g, '$1 $2'); exports.splitCamelCaseWords = splitCamelCaseWords; /** * Build the hexadecimal string representation of the passed byte array * * @param {Buffer} b - The byte array array to use * @returns the hexadecimal string */ const toHex = (b) => b.toString('hex'); exports.toHex = toHex; /** * Applies the XOR function to two strings in the sense that each charCode are XORed * * @param {string} s1 - The first string to use * @param {string} s2 - The second string * @returns the XORed result * @throws strings must be of the same size */ const xor = (s1, s2) => { if (s1.length !== s2.length) { throw new Error('strings must be of the same size'); } return Array.from(s1).reduce((xored, c, idx) => xored + String.fromCharCode(c.charCodeAt(0) ^ s2.charCodeAt(idx)), ''); }; exports.xor = xor; //# sourceMappingURL=string.js.map