@tofusoup429/pubfuncs

Steve Kim
4 min readMar 24, 2021

This is a node project module I have created and managed. It is a collection of trivial functions I often use in multiple projects. This blog is documentation of the module explaining how to use each function inside this module and why I made them.

I made this module in order to prevent myself from coding the same functions in different projects over and over. Accordingly, the functions here are all for general purposes.

I know that I could simply use great modules created by other great programmers such as lodash. But I strongly believe the statement “if you do not understand, do not use it”. I thought It would be a lot productive and safer to make them by myself than to figure out what others made.

Making it the number one rule not to include any 3rd-party dependencies at all, I tries to keep this module as small as possible.

The source code of this module can be found at my github repository at https://github.com/tofusoup429/pubfuncs.

The latest version as of writing this is 1.0.11 with 10 functions its unpacked sized is just 16.9kb.

  1. How to install

As everyone knows, this module can be installed.

yarn add @tofusoup429/pubfuncs

or

npm install @tofusoup429/pubfuncs

2. Three subcategory as of v1.0.10: Cryptologies, Dates and Objects.

(1) Cryptologies handles encrypting, decrypting and hash values.

(2) Dates include functions that deal with Date object.

(3) Objects include functions that handle objects.

Despite the three categories, you can just import this module from the root as follows.

import {functionName1, functionName2} from @tofusoup429/pubfuncs

3. Functions in v1.0.10

import {encrypt} from “@tofusoup429/pubfuncs”

  • encrypt(_text:string, _password:string, _alg:‘aes-256-cbc’|’aes-192-cbc’) returns “base64 string”

_text: text that you want to cipher.

_password: password

_alg: “aes-256-cbc”||“aes-192-cbc”. Default is “aes-256-cbc

I found handling AES encryption and decryption quite complicated by using only “crypto”, a built-in node module at “https://nodejs.org/api/crypto.html”.

This is one-liner function that encrypts a text and returns the encrypted text in base64.

import {decrypt} from “@tofusoup429/pubfuncs”

  • decrypt(_encrypted:string, _password:string, alg:‘aes-256-cbc’|’aes-192-cbc’: returns “utf-8 string”

This is the opposite of the aforementioned encrypt function. This is one-liner function that decrypt a ciphered text with this functions.

import {getNumberFromText} from @tofusoup429/pubfuncs

  • getNumberFromText(_text:string, maxInt:number): returns number

_text: a string value that you want to numberize

_maxInt: a max number//default: 4,294,967,294

This function converts a text to a number. This can be used when you attempt to generate a unique numeric id from a name of someone or something.

For example, “Mike Kim” returns 1050110595 and “MIT” returns 2621175041. Since the number comes from hash values, a slight change of the text returns completely different number. “M.I.T” returns 4151893349.

It uses sha256 algorithm to convert the text to a hash value. The hash value is digested in hex and then converted to a decimal number. The remainder resulting from dividing the number with the _maxInt is returned.

There may be chance of two different texts returning the same number. In order to avoid it, you should provide a _maxInt large enough such as 4,294,967,294.

import {validateDateYYYYMMDD} from “@tofusoup429/pubfuncs”

  • validateDateYYYYMMDD(_date:string): returns boolean

This function checks if _date in a yyyymmdd-string-format is a valid date. For example, “20190229” returns false while “20200229” returns true because the year of 2019 does not have 29th day in Feb while 2020 does. Of course some silly dates such as 20201440 or 20201630 also returns false.

import {addDate} from “@tofusoup429/pubfuncs”

  • addDate(_date:string, numberOfDate:number, _dateFormat:string): returns string

_date: a string format such as “20200203”

_numberOfDate: the number of date to add to _date.

_dateFormat: “yyyymmdd”||“yyyy-mm-dd” default: “yyyymmdd”

It takes a yyyymmdd-string-format date and a number. And add those two and return a new yyyymmdd-string forat date. For example if you pass “20200229” and 1, it return “20200301”

import {splitArray} from “@tofusoup429/pubfuncs”

  • splitArray(_array:T[], _numInnerArray:number):return T[][]

_array: An array that you want to split

_numInnerArray: the number of innerArray

This function breaks a lengthy array into several inner arrays. Say you have an array

let bigArray = [{id: 1, name: Person1}, {id: 2, name: Person2}, {id: 3, name: Person3}, {id: 4, name: Person4}, {id: 5, name: Person5}, {id: 6, name: Person6}, {id: 6, name: Person7}]

Now you want to split the array into 3 small arrays.

Then you can use this functions

let smallArrays = splitArray( bigArray, 3)

The smallArrays would be

[

[{id: 1, name: Person1}, {id: 2, name: Person2}, {id: 3, name: Person3}],

[{id: 4, name: Person4}, {id: 5, name: Person5}, {id: 6, name: Person6}],

[{id: 6, name: Person7}]

]

This function is quite useful in making batchWriteItem request to AWS dynamoDB. The maximum number of batches in a request is 25.

This is what AWS DynamoDB SDK document says

The BatchWriteItem operation puts or deletes multiple items in one or more tables. A single call to BatchWriteItem can write up to 16 MB of data, which can comprise as many as 25 put or delete requests. Individual items to be written can be as large as 400 KB.

If you have more than 25 items, you should break it before sending the batchWriteItem. Using this function, splitArray(bigArray, 25) will solve the problem with just one line.

--

--

Steve Kim

A Certified Public Accountant / Hobbyist-programmer-but-dead-serious-specializing JavaScript, ReactJS, NextJS and AWS.