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

Commit 3fccd4d

Browse files
feat: hide: default mask can be overridden
a significant refactoring has been necessary to allow all tag functions to accept an object of options: ```javascript myTag({foo_enabled: true})`do something with ${x} and ${y}` ```
1 parent 59f5f53 commit 3fccd4d

File tree

13 files changed

+156
-52
lines changed

13 files changed

+156
-52
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dist/%.js: src/%.js
1111
mkdir -p $(@D)
1212
java -jar /workspaces/closure-compiler/compiler.jar \
1313
--compilation_level=SIMPLE_OPTIMIZATIONS \
14-
--language_in=ECMASCRIPT_2015 \
14+
--language_in=ECMASCRIPT_2018 \
1515
--language_out=ECMASCRIPT_2015 \
1616
--js=$^ \
1717
--js_output_file=$@

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,21 @@ defaults`Hi ${username}/guest, you have ${num}/no new emails`;
9999
### <a name="hide"></a>hide
100100

101101

102-
The `hide` tag replaces all interpolated values with 'xxx':
102+
The `hide` tag replaces all interpolated values with a default mask `'xxx'`:
103103

104104
```javascript
105105
import {hide} from '@customcommander/tagtical';
106106

107107
hide`Hi ${name}, your credit card number is ${cc_num}`
108108
//=> "Hi xxx, your credit card number is xxx"
109109
```
110+
111+
The default mask can be overridden:
112+
113+
```javascript
114+
hide({mask: '🌯'})`Hi ${name}, your credit card number is ${cc_num}`
115+
//=> "Hi 🌯, your credit card number is 🌯"
116+
```
110117
### <a name="lower"></a>lower
111118

112119

jsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"include": [
3+
"src/**/*.js"
4+
]
5+
}

src/hide.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,37 @@
2121
* SOFTWARE.
2222
*/
2323

24+
const tag_function = require('./utils/tag-function');
25+
2426
/**
2527
* Hides interpolated values
2628
*
27-
* The `hide` tag replaces all interpolated values with 'xxx':
29+
* The `hide` tag replaces all interpolated values with a default mask `'xxx'`:
2830
*
2931
* ```javascript
3032
* import {hide} from '@customcommander/tagtical';
3133
*
3234
* hide`Hi ${name}, your credit card number is ${cc_num}`
3335
* //=> "Hi xxx, your credit card number is xxx"
3436
* ```
37+
*
38+
* The default mask can be overridden:
39+
*
40+
* ```javascript
41+
* hide({mask: '🌯'})`Hi ${name}, your credit card number is ${cc_num}`
42+
* //=> "Hi 🌯, your credit card number is 🌯"
43+
* ```
44+
*
45+
* @function
3546
*/
3647
module.exports =
37-
(l, x, r) =>
38-
[ l
39-
, 'xxx'
40-
, r
41-
];
48+
tag_function
49+
( (l, x, r, {mask}) =>
50+
[ l
51+
, mask
52+
, r
53+
]
54+
/* default options */
55+
, { mask: 'xxx'
56+
}
57+
);

src/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const tag_function = require('./utils/tag_function');
1+
const tag_function = require('./utils/tag-function');
2+
const use_tag = require('./utils/use-tag');
23
const join = require('./utils/join');
34
const intersperse = require('./utils/intersperse');
45
const compose = require('./utils/compose');
@@ -11,15 +12,15 @@ const upper = require('./upper');
1112

1213
const tag = (...fns) =>
1314
(strs, ...vals) =>
14-
compose(join(''), ...fns.map(tag_function.unwrap))
15+
compose(join(''), ...fns.map(use_tag.unwrap))
1516
(intersperse(strs, vals));
1617

17-
tag.defaults = tag_function(defaults);
18-
tag.hide = tag_function(hide);
19-
tag.lower = tag_function(lower);
20-
tag.pluralize = tag_function(pluralize);
21-
tag.trim = tag_function(trim);
22-
tag.upper = tag_function(upper);
23-
tag.of = tag_function;
18+
tag.defaults = use_tag(defaults);
19+
tag.hide = use_tag(hide);
20+
tag.lower = use_tag(lower);
21+
tag.pluralize = use_tag(pluralize);
22+
tag.trim = use_tag(trim);
23+
tag.upper = use_tag(upper);
24+
tag.of = compose(use_tag, tag_function);
2425

2526
module.exports = tag;

src/lower.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* SOFTWARE.
2222
*/
2323

24+
const tag_function = require('./utils/tag-function');
25+
2426
/**
2527
* Lowercase interpolated values
2628
*
@@ -41,10 +43,12 @@
4143
* ```
4244
*/
4345
module.exports =
44-
(l, x, r) =>
45-
[ l
46-
, typeof x === 'string'
47-
? x.toLowerCase()
48-
: x
49-
, r
50-
];
46+
tag_function
47+
( (l, x, r) =>
48+
[ l
49+
, typeof x === 'string'
50+
? x.toLowerCase()
51+
: x
52+
, r
53+
]
54+
);

src/pluralize.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* SOFTWARE.
2222
*/
2323

24+
const tag_function = require('./utils/tag-function');
25+
2426
const is_pos_int = x =>
2527
Number.isInteger(x) && x >= 0;
2628

@@ -49,7 +51,6 @@ const get_plural_form = str =>
4951
? lws + plf.substring(1) + tws
5052
: lws + snf + plf.substring(1, plf.length-1) + tws);
5153

52-
5354
/**
5455
* Choose between singular or plural forms.
5556
*
@@ -97,14 +98,16 @@ const get_plural_form = str =>
9798
* 📢 A `0` will pick the __plural__ form(s).
9899
*/
99100
module.exports =
100-
(l, x, r) =>
101-
[ !is_pos_int(x) ? l
102-
: x === 1 ? get_singular_form(l)
103-
: get_plural_form(l)
101+
tag_function
102+
( (l, x, r) =>
103+
[ !is_pos_int(x) ? l
104+
: x === 1 ? get_singular_form(l)
105+
: get_plural_form(l)
104106

105-
, x
107+
, x
106108

107-
, !is_pos_int(x) ? r
108-
: x === 1 ? get_singular_form(r)
109-
: get_plural_form(r)
110-
];
109+
, !is_pos_int(x) ? r
110+
: x === 1 ? get_singular_form(r)
111+
: get_plural_form(r)
112+
]
113+
);

src/trim.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* SOFTWARE.
2222
*/
2323

24+
const tag_function = require('./utils/tag-function');
25+
2426
/**
2527
* Trim interpolated values
2628
*
@@ -36,10 +38,12 @@
3638
* ```
3739
*/
3840
module.exports =
39-
(l, x, r) =>
40-
[ l
41-
, typeof x === 'string'
42-
? x.trim()
43-
: x
44-
, r
45-
]
41+
tag_function
42+
( (l, x, r) =>
43+
[ l
44+
, typeof x === 'string'
45+
? x.trim()
46+
: x
47+
, r
48+
]
49+
)

src/types/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Tag options.
3+
* @typedef {Object} TagOptions
4+
*/
5+
6+
/**
7+
* Creates a tag function bound to a specific set of options.
8+
* @typedef {function(TagOptions): TagFunction} TagConfigFunction
9+
*/
10+
11+
/**
12+
* Tag function.
13+
* @typedef {function(string, ?, string, TagOptions): Array} TagFunction
14+
*/

src/upper.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* SOFTWARE.
2222
*/
2323

24+
const tag_function = require('./utils/tag-function');
25+
2426
/**
2527
* Uppercase interpolated values
2628
*
@@ -37,10 +39,12 @@
3739
* ```
3840
*/
3941
module.exports =
40-
(l, x, r) =>
41-
[ l
42-
, typeof x === 'string'
43-
? x.toUpperCase()
44-
: x
45-
, r
46-
];
42+
tag_function
43+
( (l, x, r) =>
44+
[ l
45+
, typeof x === 'string'
46+
? x.toUpperCase()
47+
: x
48+
, r
49+
]
50+
);

0 commit comments

Comments
 (0)