0.1.2 • Published 6 years ago

intercalate v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

intercalate is a small JavaScript library for inserting separators into arrays. It can insert separators, a final separator, and can special-case two-element arrays.

This is useful in conjunction with things like hastscript.

To load:

const intercalate = require('intercalate');

Then use with a single separator:

> intercalate(['knuth', 'euler', 'hamilton'], ' and ')
['knuth', ' and ', 'euler', ' and ', 'hamilton']

An optional final separator:

> intercalate(['My parents', 'Ayn Rand', 'God'], ', ', ' and ')
['My parents', ', ', 'Ayn Rand', ' and ', 'God']

But we really want to use an Oxford comma:

> intercalate(['My parents', 'Ayn Rand', 'God'], ', ', ', and ')
['My parents', ', ', 'Ayn Rand', ', and ', 'God']

Which looks weird with two elements:

> intercalate(['Bob', 'Larry'], ', ', ', and ')
['Bob', ', and ', 'Larry']

So intercalate lets you special-case two-item lists:

> intercalate(['Bob', 'Larry'], ', ', ', and ', ' and ')
['Bob', ' and ', 'Larry']

And the pair separator is correctly ignored for longer lists:

> intercalate(['My parents', 'Ayn Rand', 'God'], ', ', ', and ', ' and ')
['My parents', ', ', 'Ayn Rand', ', and ', 'God']

This lets us create gramatically correct lists that work as pairs and that do not introduce disturbing family trees.