0.15.0 • Published 1 month ago

@lytrax/lxlib v0.15.0

Weekly downloads
4
License
MIT
Repository
github
Last release
1 month ago

LytraX JS/TS library

A collection of useful functions/classes for string/time manipulation, math/random calculations and encryption.

Functions

bitCount32 from math

import { bitCount32 } from '@lytrax/lxlib/math';

const bitLength = bitCount32(0x55555555); // 16 flagged bits

randomColor from random

import { randomColor } from '@lytrax/lxlib/random';

const cssRandomColor = randomColor();

randomHueColor from random

import { randomHueColor } from '@lytrax/lxlib/random';

const cssRandomHueColor = randomHueColor(33, 76);

randomInt from random

import { randomInt } from '@lytrax/lxlib/random';

const randInt = randomInt(1, 100);

randomIntNotIn from random

import { randomIntNotIn } from '@lytrax/lxlib/random';

// randInt <=> 1..10 and <> 2, 4, 6
const randInt = randomIntNotIn(1, 10, [2, 4, 6]);

randomIndex from random

import { randomIndex } from '@lytrax/lxlib/random';

let index = randomIndex(10); // index range 0..9
index = randomIndex(10, { inclusive: true }); // index range 0..10
index = randomIndex(10, { startFrom: 2 }); // index range 2..9

randomId from random

import { randomId } from '@lytrax/lxlib/random';

const id = randomId(); // id range 1..0x7FFFFFFF

randomId64 from random

import { randomId64 } from '@lytrax/lxlib/random';

const id = randomId64(); // id range 1..0X7FFFFFFFFFFFFFFF

dice from random

import { dice } from '@lytrax/lxlib/random';

const diceValue = dice(6);

sleep from time

import { sleep } from '@lytrax/lxlib/time';

await sleep(500);

uts from time

import { uts } from '@lytrax/lxlib/time';

const unixTimeStamp = uts(); // UTS in seconds

utsj from time

import { utsj } from '@lytrax/lxlib/time';

const unixTimeStampMillis = utsj(); // UTS in milliseconds

timeHash from time

import { timeHash } from '@lytrax/lxlib/time';

const hash = timeHash(); // kkluv4t0

dayKey from time

import { dayKey } from '@lytrax/lxlib/time';

const key = dayKey({ year: 2021, ordinal: 111 }); // 2021111
const resolved = dayKey.resolve(key); // { year: 2021, ordinal: 111 }

monthKey from time

import { monthKey } from '@lytrax/lxlib/time';

const key = monthKey({ year: 2021, month: 7 }); // 202107
const resolved = monthKey.resolve(key); // { year: 2021, month: 7 }

dateKey from time

import { dateKey } from '@lytrax/lxlib/time';

const key = dateKey({ year: 2021, month: 7, day: 1 }); // 20210701
const resolved = dateKey.resolve(key); // { year: 2021, month: 7, day: 1 }

minuteKey from time

import { minuteKey } from '@lytrax/lxlib/time';

const key = minuteKey({ hour: 23, minute: 30 }); // 2330
const resolved = minuteKey.resolve(key); // { hour: 23, minute: 30, second: 0 }

timeKey from time

import { timeKey } from '@lytrax/lxlib/time';

const key = timeKey({ hour: 23, minute: 30, second: 59 }); // 233059
const resolved = minuteKey.timeKey(key); // { hour: 23, minute: 30, second: 59 }

timeToMinutes from time

import { timeToMinutes } from '@lytrax/lxlib/time';

const minutes = timeToMinutes({ hour: 23, minute: 30 }); // 1410

minutesTotime from time

import { minutesTotime } from '@lytrax/lxlib/time';

const time = minutesTotime(1410); // { hour: 23, minute: 30 }

toFormData from utils

import { toFormData } from '@lytrax/lxlib/utils';

const formData = toFormData({
  some: 'Some',
  values: 123
});

fetchTimeout from utils

import { fetchTimeout } from '@lytrax/lxlib/utils';

// Will timeout after 5s if fetch won't get a reply
fetchTimeout('https://myurl.data',
  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  },
  5000 // Timeout in milliseconds
);

normalizeGreek from string

import { normalizeGreek } from '@lytrax/lxlib/string';

normalizeGreek('Ελληνικό κείμενο που θα φύγουν οι τόνοι');

camelize from string

import { camelize } from '@lytrax/lxlib/string';

camelize('Some text to camelize'); // "SomeTextToCamelize"

toTitleCase from string

import { toTitleCase } from '@lytrax/lxlib/string';

toTitleCase('Some text to title case') // "Some Text To Title Case"

toSentence from string

import { toSentence } from '@lytrax/lxlib/string';

toSentence('Some Text TO SENTENCE') // "Some text to sentence"

makeLines from string

import { makeLines } from '@lytrax/lxlib/string';

makeLines({ lines: ['make', 'some', 'lines'] }) // "make↵some↵lines"

nl2br from string

import { nl2br } from '@lytrax/lxlib/string';

nl2br('Some\ntext\nwith\nlines') // "Some<br/>text<br/>with<br/>lines"

removeAllWhitespaces from string

import { removeAllWhitespaces } from '@lytrax/lxlib/string';

removeAllWhitespaces('Text   With \tWitespaces\t\t') // "TextWithWitespaces"

decodeHtmlCharCodes from string

import { decodeHtmlCharCodes } from '@lytrax/lxlib/string';

decodeHtmlCharCodes('This&#32;is&#10;some&#32;text') // "This is↵some text"

toJsonIntended from string

import { toJsonIntended } from '@lytrax/lxlib/string';

toJsonIntended({some: 'Some', one: 1})
// {
//   "some": "Some",
//   "one": 1
// }

format from string

import { format } from '@lytrax/lxlib/string';

format('My name is {name} and my IQ is {IQ}', { name: 'Christos', IQ: 111 })
// "My name is Christos and my IQ is 111"

translateBool from string

import { translateBool } from '@lytrax/lxlib/string';

translateBool('Yes') // true

quoteSingle from string

import { quoteSingle } from '@lytrax/lxlib/string';

quoteSingle('test') // "'test'"

quoteDouble from string

import { quoteDouble } from '@lytrax/lxlib/string';

quoteDouble('test') // ""test""

quoteBacktick from string

import { quoteBacktick  } from '@lytrax/lxlib/string';

quoteBacktick('test') // "`test`"

quoteLRSingle from string

import { quoteLRSingle } from '@lytrax/lxlib/string';

quoteLRSingle('test') // "‘test’"

quoteLRDouble from string

import { quoteLRDouble } from '@lytrax/lxlib/string';

quoteLRDouble('test') // "“test”"

quote from string

import { quote } from '@lytrax/lxlib/string';

quote('test', 'backtick') // "`test`"

quoteIf from string

import { quoteIf } from '@lytrax/lxlib/string';

quoteIf('', 'single') // ""

numToSSColumn from string

// Number to SpreadSheet column
import { numToSSColumn } from '@lytrax/lxlib/string';

numToSSColumn(29) // "AC"

uniqueChars from string

// Number to SpreadSheet column
import { uniqueChars } from '@lytrax/lxlib/string';

uniqueChars('ABCabcABCabc/iumi/') // "ABCabc/ium"

applyBackspaceChar from string

// Apply all backspaces to the string
import { applyBackspaceChar } from '@lytrax/lxlib/string';

applyBackspaceChar('Test small\b\b\b\b\b line!\b') // "Test line"

removeRepeatedChars from string

// Remove all/selected repeated characters from the string
import { removeRepeatedChars } from '@lytrax/lxlib/string';

removeRepeatedChars('aa bbb word aaa cat aaaa cccc') // "a b word a cat a c"
removeRepeatedChars('This   is  aaa test', ' ') // "This is aaa test"
removeRepeatedChars('This   is  aaa test', ' a') // "This is a test"

hslToRgb from color

// Apply all backspaces to the string
import { hslToRgb } from '@lytrax/lxlib/color';

hslToRgb(260, 55, 27)

Classes

Cryptr from crypto/cryptr

// https://github.com/MauriceButler/cryptr with options
import Cryptr from '@lytrax/lxlib/crypto/cryptr';

const cryptr = new Cryptr('mysecret', {
  algorithm: 'aes-256-gcm',  // 'aes-256-gcm' is the default value
  encoding: 'base64'         // 'base64' is the default value
});

const encryptedBase64 = cryptr.encrypt('Some data');
const decryptedData = cryptr.decrypt(encryptedBase64);

Development

Publish

Always commit everything before publishing new releases.

  1. yarn build to build the distribution files
  2. yarn deploy or np --contents=release to bump version, run release script and publish to NPM and GitHub

Running np will have the version script executed which will run the makeRelease script.

License

MIT LICENSE

0.15.0

1 month ago

0.14.0

2 years ago

0.11.0

2 years ago

0.12.0

2 years ago

0.13.0

2 years ago

0.13.1

2 years ago

0.10.1

3 years ago

0.10.2

3 years ago

0.10.0

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago

0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago