0.13.1 • Published 7 years ago

karmia-utility v0.13.1

Weekly downloads
83
License
MIT
Repository
github
Last release
7 years ago

karmia-utility

Karmia utility suites

Usage

const karmia_utility = require('karmia-utility').default,
    utility = new karmia_utility();

array

array

unique

  • list <Array>

Removes duplicate values from an array

count

  • list <Array>
  • item <*>

Count item in list

range

  • start <number>
  • stop <number>
  • step <number> Default: 1

Get array of integer from start to end

flip

  • list <Array>

intersection

  • list1 <Array>
  • list2 <Array>

Get intersection of two arrays

const list1 = ['a', 'b', 'b', 'c', 'c', 'c'],
    list2 = ['a', 'b'];

console.log(karray.difference(list1, list2));
// => ['a', 'b']

difference

  • list1 <Array>
  • list2 <Array>

Get difference of two arrays

const list1 = ['a', 'b', 'b', 'c', 'c', 'c'],
    list2 = ['a', 'b'];

console.log(karray.difference(list1, list2));
// => ['c', 'c', 'c']

crypto

hash

hash(algorithm, buffer, encoding)

  • algorithm <string>
  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.hash('md5', Buffer.from('text'), 'binary');

sha1(buffer, encoding)

  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.sha1(Buffer.from('text'), 'binary');

sha256(buffer, encoding)

  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.sha256(Buffer.from('text'), 'binary');

sha512(buffer, encoding)

  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.sha512(Buffer.from('text'), 'binary');

stretching(algorithm, buffer, count, encoding)

  • algorithm <string>
  • buffer <Buffer>
  • count <Number>
  • encoding <string> Input encoding
utility.crypto.stretching('sha256', Buffer.from('text'), 10000, 'binary');

hmac

hmac(algorithm, secret, buffer, encoding)

  • algorithm <string>
  • password <Buffer>
  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.hmac('md5', 'secret', Buffer.from('text'), 'binary');

hmac_sha1(secret, buffer, encoding)

  • password <Buffer>
  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.hmac_sha1('secret', Buffer.from('text'), 'binary');

hmac_sha256(secret, buffer, encoding)

  • password <Buffer>
  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.hmac_sha256('secret', Buffer.from('text'), 'binary');

hmac_sha512(secret, buffer, encoding)

  • password <Buffer>
  • buffer <Buffer>
  • encoding <string> Input encoding
utility.crypto.hmac_sha512('secret', Buffer.from('text'), 'binary');

encrypt

iv()

const iv = utility.crypto.iv();

encrypt(algorithm, password, buffer, encoding)

  • algorithm <string>
  • password <Buffer>
  • buffer <Buffer>
  • encoding <string> Input encoding
const password = Buffer.from('password'),
      data = Buffer.from('text');
utility.crypto.encrypt('aes-256-ctr', password, data, 'binary');

decrypt(algorithm, password, data, encoding)

  • algorithm <string>
  • password <Buffer>
  • data <Object> {data: encrypted}
  • encoding <string> Output encoding
const password = Buffer.from('password'),
      data = {data: 'encrypted'};
utility.crypto.decrypt('aes-256-ctr', password, data, 'binary');

encryptiv(algorythm, password, iv, data, encoding)

  • algorithm <string>
  • password <Buffer>
  • iv <Buffer>
  • buffer <Buffer>
  • encoding <string> Input encoding
const password = Buffer.from('password'),
      iv = Buffer.from('iv'),
      buffer = Buffer.from('text');
utility.crypto.encryptiv('aes-256-gcm', password, iv, data, 'binary');

decryptiv(algorithm, password, iv, data, encoding, tag_encoding)

  • algorithm <string>
  • password <Buffer>
  • iv <Buffer>
  • data <Object> {data: encrypted} or {data: encrypted, tag: auth_tag}
  • encoding <string> Output encoding
  • tag_encoding <string> Auth tag input encoding
const password = Buffer.from('password'),
    iv = Buffer.from('iv'),
    data = {
        data: 'encrypted',
        tag: 'auth_tag'
    };
utility.crypto.decryptiv(algorithm, password, iv, data, 'binary', 'binary');

date

date

getDate()

utility.date.getDate();

getTime()

utility.date.getTime();

getYMD()

utility.date.getYMD();

format(format, date)

// Current time
utility.date.format('Y-m-d H:i:s');

// Specific time
utility.date.format('Y-m-d H:i:s', new Date('1999-08-03 00:00:00'));
formatdescriptionexample
dDay of the month, 2 digits with leading zeros01-31
DA textual representation of a day, three lettersMon through Sun
jDay of the month without leading zeros1 to 31
lA full textual representation of the day of the weekSunday through Saturday
NISO-8601 numeric representation of the day of the week1 (for Monday) through 7 (for Sunday)
SEnglish ordinal suffix for the day of the month, 2 charactersst, nd, rd or th. Works well with j
wNumeric representation of the day of the week0 (for Sunday) through 6 (for Saturday)
zThe day of the year (starting from 0)0 through 365
WISO-8601 week number of year, weeks starting on MondayExample: 42 (the 42nd week in the year)
FA full textual representation of a month, such as January or MarchJanuary through December
mNumeric representation of a month, with leading zeros01 through 12
MA short textual representation of a month, three lettersJan through Dec
nNumeric representation of a month, without leading zeros1 through 12
tNumber of days in the given month28 through 31
LWhether it's a leap year1 if it is a leap year, 0 otherwise.
oISO-8601 year number.Examples: 1999 or 2003
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
aLowercase Ante meridiem and Post meridiemam or pm
AUppercase Ante meridiem and Post meridiemAM or PM
BSwatch Internet time000 through 999
g12-hour format of an hour without leading zeros1 through 12
G24-hour format of an hour without leading zeros0 through 23
h12-hour format of an hour with leading zeros01 through 12
H24-hour format of an hour with leading zeros00 through 23
iMinutes with leading zeros00 to 59
sSeconds, with leading zeros00 through 59
uMicrosecondsExample: 654000
ODifference to Greenwich time (GMT) in hoursExample: +0200
PDifference to Greenwich time (GMT) with colon between hours and minutesExample: +02:00
TTimezone abbreviationExamples: EST, MDT ...
ZTimezone offset in seconds-43200 through 50400
cISO 8601 date2004-02-12T15:19:21+00:00
rRFC 2822 formatted dateExample: Thu, 21 Dec 2000 16:01:07 +0200
USeconds since the Unix Epoch (January 1 1970 00:00:00 GMT)

random

random

string(length, options)

  • length <number>
  • options <Object>
// Includes all characters
utility.string.random(16);

// Excludes special characters
utility.string.random(16, {special: false});

// Excludes number
utility.string.random(16, {number: false});

// Exclude upper and special characters
utility.string.random(16, {upper: false, special: false});

// Exclude lower and special characters
utility.string.random(16, {lower: false, special: false});

integer(max, min)

  • max <number> Default: Number.MAX_SAFE_INTEGER
  • min <number> Default: 0
utility.random.integer(); // 0〜Number.MAX_SAFE_INTEGER

sequence

sequence

constructor

  • adapter <object> KarmiaUtilitySequenceAdapter object Default: KarmiaUtilitySequenceAdapterNumber
  • initial_value <*> Default: undefined
const sequence = new utility.sequence();

get

const sequence = new utility.sequence();
await utility.get();

string

string

strip

  • string <string>
  • mask_character <string> Default: " \t\n\r\0\x0B"
// Strip whitespaces
const string = '    Hello, world.    ';
utility.string.strip(string);

// Strip specified characters
const string = '"Hello, world."';
utility.string.strip(string, '"');

lstrip

  • string <string>
  • mask_character <string> Default: " \t\n\r\0\x0B"
// Strip whitespaces
const string = '    Hello, world.';
utility.string.lstrip(string);

// Strip specified characters
const string = '"Hello, world.';
utility.string.lstrip(string, '"');

rstrip

  • string <string>
  • mask_character <string> Default: " \t\n\r\0\x0B"
// Strip whitespaces
const string = 'Hello, world.    ';
utility.string.rstrip(string);

// Strip specified characters
const string = 'Hello, world."';
utility.string.rstrip(string, '"');

normalize

  • string <string>
  • form <string> Default: "NFKC"
const string = '\u202b123\r\nABC\rdef\nアイウエオガ';
utility.string.normalize(string); // Return: '123\nABC\ndef\nアイウエオガ'

unquote

  • string <string>
const string = '"Hello, world."';
utility.string.unquote(string);

parse

  • string <string>
  • delimiter <string/regexp> Default: /,? /
  • separator <string/regexp> Default: =
const string = 'key1=value1, key2=value2';
utility.string.parse(string, /,? /, '=');

toBoolean

  • string <string>
// Should be true
utility.string.toBoolean('true');
utility.string.toBoolean('True');
utility.string.toBoolean('TRUE');
utility.string.toBoolean('0');
utility.string.toBoolean(1);
utility.string.toBoolean(true);
utility.string.toBoolean('false_1');

// Should be false
utility.string.toBoolean('false');
utility.string.toBoolean('False');
utility.string.toBoolean('FALSE');
utility.string.toBoolean('');
utility.string.toBoolean(0);
utility.string.toBoolean(false);
0.13.1

7 years ago

0.13.0

7 years ago

0.12.0

7 years ago

0.11.7

8 years ago

0.11.6

8 years ago

0.11.5

8 years ago

0.11.4

8 years ago

0.11.3

8 years ago

0.11.2

8 years ago

0.11.1

8 years ago

0.11.0

8 years ago

0.10.2

8 years ago

0.10.1

8 years ago

0.10.0

8 years ago

0.9.4

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

9 years ago

0.9.0

9 years ago

0.8.6

9 years ago

0.8.5

9 years ago

0.8.4

9 years ago

0.8.3

9 years ago

0.8.2

9 years ago

0.8.1

9 years ago

0.8.0

9 years ago

0.7.0

9 years ago

0.6.3

9 years ago

0.6.2

9 years ago

0.6.1

9 years ago

0.6.0

9 years ago

0.5.10

9 years ago

0.5.9

9 years ago

0.5.8

9 years ago

0.5.7

9 years ago

0.5.6

9 years ago

0.5.5

9 years ago

0.5.4

9 years ago

0.5.3

9 years ago

0.5.2

9 years ago

0.5.1

9 years ago

0.5.0

9 years ago

0.3.1

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.0

10 years ago

0.0.11

10 years ago

0.0.10

10 years ago

0.0.9

10 years ago

0.0.8

10 years ago

0.0.7

10 years ago

0.0.6

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago

0.0.3

10 years ago