Have ideas to improve npm?Join in the discussion! »

    ts-utls
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.1 • Public • Published

    ts-utls

    Github tag (latest by date) npm Github last commit Github issues NPM

    ts-utls is a small TypeScript library where I put all useful stuff I regularly need in my projects. Feel free to use at your discretion with the apppropriate license mentions.

    Usage

    npm i ts-utls

    This library contains the following functions:

    • For arrays:
      • chunk: split an array into chunks of a maximum size;
      • flatten: transform an array of arrays of items to an array of items;
      • groupBy: group an array of items by some item's field;
      • range: return a list of integers;
    • For bits and buffers:
      • buffer2BytesString: transform a byte array to its string representation of byte(s);
      • int2Buffer: convert an integer to its byte array equivalent;
      • splitBuffer: split a byte array using a passed byte array;
      • stringBytes2Buffer: transform a string representing one or more bytes to a byte array;
    • For numbers:
      • euclideanDivision: compute the euclidean division of two integers, returning the quotient and the remainder;
    • For strings:
      • capitalize: capitalize the first letter of a sentence;
      • fromHex and toHex: transform hexadecimal string representation to byte array, and vice-versa;
      • hashCode: compute the equivalent of Java's hashCode;
      • shuffle: randomly shuffle the characters;
      • splitCamelCaseWords: put a space between each "word" found in a camel-case string;
      • xor: apply the XOR logical function to two strings in the sense that each charCode is xored;
    • For time:
      • currentTimestampMillis: return the current Unix timestamp in milliseconds;
      • sleep: hold the current thread for a while.

    eg.

    import {
      buffer2BytesString, capitalize, chunk, currentTimestampMillis, flatten, groupBy, euclideanDivision, fromHex,
      int2Buffer, hashCode, range, shuffle, sleep, splitBuffer, splitCamelCaseWords, stringBytes2Buffer, toHex, xor
    } from 'ts-utls'
    
    // For arrays
    
    const arr = [1, 2, 3, 4, 5]
    const chunked = chunk(arr)
    // [[1, 2], [3, 4], [5]]
    console.log(chunked)
    
    const arrs = [[1, 2], [3, 4], [5]]
    const flattened = flatten(arrs)
    // [1, 2, 3, 4, 5]
    console.log(flattened)
    
    const arr = [{ field1: '1', field2: 1 }, { field1: '1', field2: 2 }, { field1: '3', field2: 3 }]
    const grouped = groupBy(arr, 'field1')
    console.assert(grouped['1'].length === 2)
    
    const firstFive = range(0, 5)
    // [0, 1, 2, 3, 4]
    console.log(firstFive)
    const evenBefore10 = range(0, 10, 2)
    // [0, 2, 4, 5, 6, 8]
    console.log(evenBefore10)
    
    // For bits and buffers
    
    const buf0 = Buffer.from([0, 1, 128, 2, 3])
    const delimiter = Buffer.from([128])
    const splitsWithout = splitBuffer(buf0, delimiter, false)
    // [[0, 1], [2, 3]]
    console.log(splitsWithout)
    const splitsWith = splitBuffer(buf0, delimiter, true)
    // [[0, 1], [128], [2, 3]]
    console.log(splitsWith)
    
    const buf1 = int2Buffer(1)
    console.assert(buf1[0] === 1)
    
    const str = '11011010'
    const buf2 = stringBytes2Buffer(str)
    console.assert(buf1[0] === 218)
    const str2 = buffer2BytesString(buf2)
    console.assert(str === str2)
    
    // For numbers
    
    const n = 15, d = 2
    const [q, r] = euclideanDivision(n, d)
    console.assert(q === 7 && r === 1)
    
    // For strings
    
    const phrase = capitalize('my sentence is capitalized')
    console.assert(phrase === 'My sentence is capitalized')
    
    const h = hashCode('Hello')
    console.assert(h === 69609650)
    
    const hexStrings = ['ff']
    const buffers = hexStrings.map(fromHex)
    const strings = buffers.map(toHex)
    console.assert(hexStrings[0] === strings[0])
    
    const str = 'abcd'
    const shuffled = shuffle(str)
    console.assert(str.length === shuffled.length)
    console.assert(str !== shuffled)
    
    const sentence = splitCamelCaseWords('myCamelCase')
    console.assert(sentence === 'my Camel Case')
    
    const a = 'a'
    const b = 'b'
    const xored = xor(a, b)
    console.assert(xored === '\u0003')
    
    // For time
    
    const ts = currentTimestampMillis()
    await sleep(100)
    console.assert(currentTimestampMillis() > ts + 100)

    Please let me know if you have more optimized implementations of any of my stuff.

    License

    This library is distributed under an MIT license. See the LICENSE file.


    © 2020-2021 Cyril Dever. All rights reserved.

    Install

    npm i ts-utls

    DownloadsWeekly Downloads

    10

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    38.8 kB

    Total Files

    23

    Last publish

    Collaborators

    • avatar