absolute_utils README.md
3.2 kB · 177 lines
Markdown
at main

absolute_utils #

Description #

This is a library of useful generic functions that I found myself copy & pasting into projects so I decided to centralize them and maintain them in sync and in a single repo. Some are inspired by the robust Ruby/Rails ecosystem of convenience methods, some are borne out of necessity. All methods should be non-mutative and return a value (exceptions for the latter rule may be made for timer functions), and will typically expect at least one argument.

Table of Contents #

Installation #

npm i @absoluteboi/absolute-utils

or (recommended)

yarn add @absoluteboi/absolute-utils

Usage #

Simply install and import:

numericSort

import { numericSort } from "@absoluteboi/absolute-utils"

let numbers = [5,8,1,3,7,6];

numericSort(numbers)
// [1,3,5,6,7,8]

numericSort(numbers, { reverse: true })
// [8,7,6,5,3,1]

pluck

import { pluck } from "@absoluteboi/absolute-utils"

let items = [
  { a: "foo", b: "bar" },
  { a: "baz", b: "qux" }
];

pluck(items, "a")
// ["foo", "baz"]

pluck(items, ["a", "b"])
// [["foo", "bar"], ["baz", "qux"]]

sliceEach

import { sliceEach } from "@absoluteboi/absolute-utils"

let items = [
  { a: "foo", b: "bar" },
  { a: "baz", b: "qux" }
];

sliceEach(items, "a")
// [{ a: "foo" }, { a: "baz" }]

sliceEach(items, ["a", "b"])
// [{ a: "foo", b: "bar" }, { a: "baz", b: "qux" }]

delay

import { delay } from "@absoluteboi/absolute-utils"

delay(500)
// *pauses thread execution for 500 miliseconds*

delay(5, "s")
// *pauses thread execution for 5 seconds*

delay(1, "min")
// *pauses thread execution for 1 minute*

localeDateStringListFromRange

import { localeDateStringListFromRange } from "@absoluteboi/absolute-utils"

const startDate = new Date(2023, 2, 8);
const endDate = new Date(2023, 2, 10);

localeDateStringListFromRange({ from: startDate, to: endDate })
// ["2023-03-08", "2023-03-09", "2023-03-10"]

buildQueryString

import { buildQueryString } from "@absoluteboi/absolute-utils"

const params = { foo: "bar", baz: ["qux", "quo"] };

buildQueryString(params)
// "?foo=bar&baz%5B%5D=qux&baz%5B%5D=quo"

rejectEmpty

import { rejectEmpty } from "@absoluteboi/absolute-utils"

const object = { foo: 1, bar: null, baz: 3, qux: false };

rejectEmpty(object)
// { foo: 1, baz: 3, qux: false }

fib

import { fib } from "@absoluteboi/absolute-utils"

fib(6)
// 8

fib(13)
// 233

truncateWithEllipses

import { truncateWithEllipses } from "@absoluteboi/absolute-utils"

const string = "The human torch was denied a bank loan.";

truncateWithEllipses(string, 15)
// "The human torch…"

Tests #

Run the test suite with either

npm run jest --watchAll

or (recommended)

yarn jest --watchAll

Credits #

Big thanks to the maintainers of jest, node, and typescript for this tool is built on their shoulders.

License #

GNU General Public License v3.0 or later (https://spdx.org/licenses/GPL-3.0-or-later.html)