This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: deprecate tags composition and user-defined tags
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
1 parent
88c2747
commit 2717e65
Showing
6 changed files
with
80 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters