Skip to content
This repository has been archived by the owner on Jun 21, 2024. It is now read-only.

Commit

Permalink
refactor: deprecate tags composition and user-defined tags
Browse files Browse the repository at this point in the history
This simplifies a few things and some code has been deleted as a result.

BREAKING CHANGE: users cannot define their own tags or compose tags anymore

These two features have never been "advertised" anyway so there is
very little risk that anybody ever used them. However this is
in effect a breakin change and therefore warrants a major release.
  • Loading branch information
customcommander committed Jan 21, 2021
1 parent 88c2747 commit 2717e65
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 157 deletions.
43 changes: 10 additions & 33 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,13 @@
* @copyright (c) 2021 Julien Gonzalez <[email protected]>
*/

const tag_function = require('./utils/tag-function');
const use_tag = require('./utils/use-tag');
const intersperse = require('./utils/intersperse');
const defaults = require('./defaults');
const hide = require('./hide');
const list = require('./list');
const lower = require('./lower');
const pluralize = require('./pluralize');
const time = require('./time');
const trim = require('./trim');
const upper = require('./upper');

const
{ compose
, join
} = require('./utils/fp');

const tag = (...fns) =>
(strs, ...vals) =>
compose(join(''), ...fns.map(use_tag.unwrap))
(intersperse(strs, vals));

tag.defaults = use_tag(defaults);
tag.list = use_tag(list);
tag.hide = use_tag(hide);
tag.lower = use_tag(lower);
tag.pluralize = use_tag(pluralize);
tag.time = use_tag(time);
tag.trim = use_tag(trim);
tag.upper = use_tag(upper);
tag.of = compose(use_tag, tag_function);

module.exports = tag;
module.exports = {
defaults: require('./defaults'),
hide: require('./hide'),
list: require('./list'),
lower: require('./lower'),
pluralize: require('./pluralize'),
time: require('./time'),
trim: require('./trim'),
upper: require('./upper')
};
19 changes: 0 additions & 19 deletions src/utils/intersperse.js

This file was deleted.

76 changes: 70 additions & 6 deletions src/utils/tag-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,79 @@
* @copyright (c) 2021 Julien Gonzalez <[email protected]>
*/

const {cont, init} = require('./fp');

/**
* Put `ys` inside `xs` at regular intervals.
*
* @example
* intersperse([1, 3, 5], [2, 4])
* //=> [1, 2, 3, 4, 5]
*
* @param {Array} xs
* @param {Array} ys
* @return {Array}
*/
const intersperse =
(xs, ys) =>
init(xs.flatMap((x, i) => [x, ys[i]]));

/**
* Given `fn` a function that takes three parameters and returns
* a tuple 'x' of three elements, apply `fn` to tuples 'y' of three elements.
* The first tuple 'y' is made of the first three elements of `xs`.
* The next tuple 'y' (and all others) takes its first element from the
* last element of tuple 'x' returned by `fn` when applied to the previous tuple 'y'.
* The last two elements of tuple 'y' are taken from the next two consecutive elements of `xs`.
*
* Example:
*
* ```javascript
* const fn = (a, b, c) => [a+1, b+2, c+3];
* const xs = [10, 20, 30, 40, 50, 60];
* transformer(fn, xs);
* // [10, 20, 30, 40, 50, 60, 70]
* // ^^ ^^ ^^
* // [11, 22, 33, 40, 50, 60, 70]
* // ^^ ^^ ^^
* // [11, 22, 34, 42, 53, 60, 70]
* // ^^ ^^ ^^
* //=> [11, 22, 34, 43, 54, 62, 73]
* ```
*
* @param {function()} fn
* @param {Array} xs
* @param {number} [i=1]
* @return {Array}
*/
const transformer =
(fn, xs, i=1) =>
i >= xs.length
? xs
: cont(fn(...xs.slice(i-1, i+2)))
( ret =>
transformer
( fn
, xs.slice(0, i-1).concat(ret, xs.slice(i+2))
, i+2
));

const read_user_config =
fn => (strings_or_config, ...values) =>
Array.isArray(strings_or_config)
? fn({}, strings_or_config, values)
: (strings, ...values) => fn(strings_or_config, strings, values);

/**
* @param {TagFunction} fn
* @param {!TagOptions=} opts
* @return {TagFunction|TagConfigFunction}
*/
module.exports =
(fn, opts = {}) => (...args) =>
{ const is_config_call = typeof args[0] === 'object'; // foo`…` vs foo({…})`…`
return (is_config_call
? (l, x, r) => fn(l, x, r, {...opts, ...args[0]})
: fn(...args, opts));
};
(fn, default_config = {}, postprocess = xs => xs.join('')) =>
read_user_config((user_config, strings, values) =>
{ const final_config = {...default_config, ...user_config};
const parts = intersperse(strings, values);
const preprocess = (l, x, r) => fn(l, x, r, final_config);
return postprocess(transformer(preprocess, parts));
});
20 changes: 0 additions & 20 deletions src/utils/transform.js

This file was deleted.

32 changes: 0 additions & 32 deletions src/utils/use-tag.js

This file was deleted.

47 changes: 0 additions & 47 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const test = require('tape');
const tag = require('./dist');
const
{ defaults
, hide
Expand All @@ -11,15 +10,6 @@ const
, upper
} = require('./dist');

test('tag: can compose other tags', t => {
t.plan(1);

t.is
( tag(upper, trim)`foo=${' foo '}, bar=${' bar '}`
, "foo=FOO, bar=BAR"
);
});

test('defaults: replace empty values', t => {
t.plan(2);

Expand All @@ -36,43 +26,6 @@ test('defaults: replace empty values', t => {
);
});

test('tag: can compose user-defined tags', t => {
t.plan(1);

const myTag =
tag.of(
(l, x, r) =>
[ '|'
, x
, '|'
]);

t.is
( tag(upper, myTag, trim)`foo=${' foo '}, bar=${' bar '}`
, "|FOO|BAR|"
);
});

test('user-defined tags can receive options', t => {
t.plan(1);

const myTag =
tag.of
( (l, x, r, {foo}) =>
[ l
, foo
, r
]
, { foo: 'fooooooo'
}
);

t.is
( myTag({foo: 'baaar'})`Hello ${'world'}!`
, 'Hello baaar!'
);
});

test('hide: hides all values', t => {
t.plan(5);

Expand Down

0 comments on commit 2717e65

Please sign in to comment.