0.1.2 • Published 3 years ago

hofkit v0.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

hofkit

hofkit (Higher Order Function toolKIT) is a modular functional utility library for JavaScript & TypeScript. This library collects frequently rewritten experimental functional wrappers for commonly used utility functions (either custom or from popular libraries such as lodash). These wrappers emphasised functional concepts (e.g., higher order functions, state purity, etc.) to provide functional benefits (e.g., less state, composition) whilst preserving compatibility with existing imperative approaches.

Through this flexiblity, the toolkit aims to be reusable across projects for a wide variety of uses ranging from array & object manipulation (e.g., filtering object entries) to concurrency-controlled Promises.

Additional benefits include builtin type support, environment-specific modules (i.e., featureGroups that group functions to separate dependencies), live-reloadable per-function test suites, and auto-generated live-reloadable documentation.

Features

FeatureDetails
Provide a library of functional utility functionsIncludes 164 core functions & 14 node-environment functions (as of initial release), that cover a variety of uses ranging from array & object manipulation (e.g., filtering object entries) to concurrency-controlled Promises
Compatible with function & imperative approachesProvides functional benefits (e.g., purity, composition) whilst remaining compatibility with imperative implementations
Interoperable with JavaScript & TypeScript-
Builtin TypeScript support for all functionsAll functions are written in TypeScript to generate bundled environment-specific declaration files
Typed return values for functional composition-
Auto-generated live-reloadable documentationDocumentation is generated & live-reloadable for all featureGroup functions, which includes type signatures & any manual documentation
Modular environment-specific functionsEnvironment-specific functions are grouped into individually importable featureGroup modules (e.g., core, node, etc.) to separate dependencies
Environment-specific per-function unit test suitesAll functions are easily testable via dedicated environment grouped live-reloadable test suites (totalling 180 test suites & 614 tests as of initial release)
Supports testing curried functions via command lineSee test:cli in Scripts
Consistent code via JavaScript & TypeScript lintingUses eslint-config-jsx to ensure consistent code

Usage

Install:

npm install hofkit

Using core functions in TypeScript:

import * as H from 'hofkit'; // Import all `core` functions.
// JavaScript: const H = require('hofkit');

import { titleCase } from H; // Named import.
// JavaScript: const { titleCase } = H;

titleCase('foo bar'); //=> 'Foo Bar'

// Typed return value: `toTitle: any => string`.
const toTitle = H.compose(
    titleCase,
    H.trim,
);
toTitle(' Foo-barBaz '); //=> 'Foo Bar Baz'

Using modular environment-specific functions (currently only node is supported), e.g., node functions:

import * as H from 'hofkit';
import * as HN from 'hofkit/dist/node'; // Import all `node` functions (requires Node environment).
import { readFile } from HN; // Named import.

// Using `async` IFFE for clarity but can be replaced by `H.promiseTryOr` to handle errors.
(async () => {
    // Writing to a file.
    await HN.writeFile('path/to/file')('foo');

    // Find existing paths.
    const filePaths = ['path1', 'path2', 'path3']; // Several file paths.
    const existingFilePaths = await H.promiseFilter(HN.pathExists);

    // Reading data for all existing files.
    let contents: string[] = [];
    const poolSize = 10;
    const onPoolComplete = (poolData: string) => [...contents, ...poolData]; // Flattens data from pools.
    const toFileContents = H.compose(
        H.promiseAll(poolSize, onPoolComplete), // Controlled concurrency.
        H.map(readFile)
    );
    toFileContents(existingFilePaths); //=> [['content1', ...], ...]

    // Overwriting all files.
    const writeFoo = (filePath: string) => HN.writeFile(filePath)('foo');
    const overwriteExistingFiles = H.compose(
        H.promiseAll(), // Uncontrolled concurrency;
        H.map(HN.writeFoo)
    );
    overwriteExistingFiles(filePaths);
})();

Documentation

Function documentation is available via the documentation website.

Screenshot of core documentation website:

<div align="center"><img src="https://user-images.githubusercontent.com/11654341/116275988-fea56400-a77b-11eb-9133-5ee54d8905ed.png" width="100%"/></div>

Scripts

ScriptInfo
testRun all tests
test:coreRun all core feature group tests
test:nodeRun all node feature group tests
test <...test-names>Run tests matching test-names
test:watchRun all tests in watch mode
test:cli <functionName> <...argSets>Runs a function matching functionName with argSets (see [runFunctionFromCli.ts](./src/util/runFunctionFromCli.ts))
test:data:resetResets the test data directory (i.e., _testDataDir/) & its contents
docs:cleanRemove existing docs
docs:buildOutput documentation to [docs/](./docs)
docs:build:serveOutput & serve docs
docs:serveServe existing docs in watch mode
docs:serve:openOpen served docs
docs:watchOutput docs in watch mode
docs:watch:serveOutput & serve docs in watch mode
build:cleanRemoves the generated dist/ directory
build:featureGroupIndexesUpdates all feature group index.ts files (i.e., updates exported functions, see [updateFeatureGroupIndexes.ts](./src/util/updateFeatureGroupIndexes.ts))
buildGenerates the production dist/ directory & its contents (minified bundles, bundled declarations, etc.)
build:devDevelopment alternative to build
pack:cleanRemoves generated tarball files
packRuns pack:clean before re-generating a tarball file containing npm required (e.g., package.json) & generated files (i.e., _dist/
pack:drySame as pack but prints tarball file contents without generating the tarball file
deploy:preRuns all pre-deploy scripts (e.g., test, build, pack, etc.)
deploy:majorRuns deploy:pre before publishing an incremented major version
deploy:minorRuns deploy:pre before publishing an incremented minor version
deploy:patchRuns deploy:pre before publishing an incremented patch version
deployRuns deploy:pre before publishing the current version

Future Plans

  • Complete remaining [spec](./spec.md) functions
  • Test async composition
  • Support web environments (e.g., browser, react, etc.)
  • Changelog