A light weight functional JavaScript library that only depends on itself.
Why The Name Kyanite?
Because I think mineral names are cool
Contents
Philosophy
The goal for Kyanite is to be stripped down, light weight, and intuitive. The idea is to be performant and easy to use in a functional setting, making it simple to build reusable functions in your code base with powerful and organized algorithmic pipes. Ultimately, Kyanite’s continued growth and improvement will support the growth and improvement of the JavaScript community
Key Features
- Purely Functional - This was a main focus for the project. I wanted it to be an easy to use, functional system while also being completely pure by making use of piping and transducers to boost performance.
- Single type utility functions - Theoretically, all of the functionality is based around accepting a single data type, doing what it does, and giving you back a result, thus making it reliable, stable, and lightweight.
- Everything is curried! Setup static in one spot and then pass the rest of the dynamic data in later.
- Data last ideology
How To
Note: As of
v4.0.0the script tag build adds a global namedkyaniteinstead ofK
With a CDN
<!-- It is recommended to replace the @latest with a strict version number for production -->
<script src="https://cdn.jsdelivr.net/npm/kyanite@latest/dist/kyanite.iife.min.js"></script>
<script>
kyanite.isEmpty({})
// Or give it a shorter name
const K = kyanite
</script>
Or installing with npm i kyanite and then:
Standard module system
import * as K from 'kyanite'
// Or just the functions you need
import { map, pipe } from 'kyanite'
Common JS
const K = require('kyanite')
Transducers
A chain like pipe([map(inc), filter(isEven), take(3)], list) builds a whole new array at every step, even though take only needs the first few values. Transducers run the same steps in a single pass instead: each value goes through every step before the next one is read, no in between arrays are built, and once take has enough nothing else in the list is looked at.
Leave the list off a list function and hand it to transduce or into. The steps go in an array and run first to last, just like pipe:
import { filter, into, map, take, transduce } from 'kyanite'
const isEven = n => n % 2 === 0
// into collects the results into an array, string, or object
into([], [map(n => n + 1), filter(isEven), take(3)], [1, 2, 3, 4, 5, 6, 7, 8]) // => [2, 4, 6]
// transduce takes a reducer, the same kind reduce does
transduce([filter(isEven), take(3)], (n, acc) => acc + n, 0, [1, 2, 3, 4, 5, 6, 7, 8]) // => 12
They work with any iterable, even a generator that never ends, since take stops reading once it has enough.
These work as transducers: map, filter, reject, take, takeWhile, drop, dropWhile, uniq, uniqBy, concatMap, and pluck. You can stop early from your own reducer with reduced.
Kyanite follows the standard transducer protocol, so transducers and transformers from other libraries that follow it work with transduce, and Kyanite's work with theirs.
Testing
Kyanite uses the built in Node.js test runner (node:test) with node:assert
To run the tests:
git clonethe repocdinto the repo folder- Run
npm i - Run
npm t
Credit
A lot of the if not most of the inspiration for this library came from 2 libraries I follow closely, Primarily most of it stems from:
- foreword by Abstract Tools which is a very nice and easy to use library developed by a close friend and mentor. This is where a lot of functionality ideas came from I can't recommend it enough.
- Ramdajs by Ramda a beautiful and feature packed library where the original idea started