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

Commit c13c4b5

Browse files
feat: implements trim, upper tags and allows tags to be composed
1 parent 1fb0179 commit c13c4b5

File tree

18 files changed

+254
-30
lines changed

18 files changed

+254
-30
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,41 @@
11
## API
22

3-
* <a name="#hide"></a>hide - _Hides interpolated values_
3+
* <a name="hide">hide</a> - _Hides interpolated values_
4+
* <a name="trim">trim</a> - _Trim interpolated values_
5+
* <a name="upper">upper</a> - _Uppercase interpolated values_
46

5-
### hide
7+
### <a name="#hide"></a>hide
68

7-
Hides interpolated values
89

910
The `hide` tag replaces all interpolated values with 'xxx':
1011

1112
```javascript
1213
hide`Hi ${name}, you credit card number is ${cc_num}`
1314
//=> "Hi xxx, you credit card number is xxx"
1415
```
16+
### <a name="#trim"></a>trim
17+
18+
19+
Trim all interpolated values if they are strings.
20+
Non-string values are left as is.
21+
22+
```javascript
23+
const name = ' John ';
24+
trim`My name is ${name}!`;
25+
//=> "My name is John!"
26+
```
27+
### <a name="#upper"></a>upper
28+
29+
30+
Uppercase all interpolated values if they are strings.
31+
Non-string values are left as is.
32+
33+
```javascript
34+
const name = 'john';
35+
const age = 40;
36+
upper`My name is ${name} and I am ${age} years old`
37+
//=> "My name is JOHN and I am 40 years old"
38+
```
1539

1640
## Contributing
1741

index.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1+
const tag_function = require('./src/utils/tag_function');
2+
const join = require('./src/utils/join');
3+
const intersperse = require('./src/utils/intersperse');
4+
const compose = require('./src/utils/compose');
15
const hide = require('./src/hide');
6+
const trim = require('./src/trim');
7+
const upper = require('./src/upper');
28

3-
module.exports = {
4-
hide
5-
};
9+
const tag = (...fns) =>
10+
(strs, ...vals) =>
11+
compose(join(''), ...fns.map(tag_function.unwrap))
12+
(intersperse(strs, vals));
13+
14+
tag.hide = tag_function(hide);
15+
tag.trim = tag_function(trim);
16+
tag.upper = tag_function(upper);
17+
tag.of = tag_function;
18+
19+
module.exports = tag;

index.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const test = require('tape');
2+
const tag = require('./');
3+
const {trim, upper} = tag;
4+
5+
test('tag: can compose other tags', t => {
6+
t.plan(1);
7+
8+
const foo = ' foo ';
9+
const bar = ' bar ';
10+
11+
t.is
12+
( tag(upper, trim)`foo=${foo}, bar=${bar}`
13+
, "foo=FOO, bar=BAR"
14+
);
15+
});
16+
17+
test('tag: can compose user-defined tags', t => {
18+
t.plan(1);
19+
20+
const foo = ' foo ';
21+
const bar = ' bar ';
22+
23+
const myTag =
24+
tag.of(
25+
(l, x, r) =>
26+
[ '|'
27+
, x
28+
, '|'
29+
]);
30+
31+
t.is
32+
( tag(upper, myTag, trim)`foo=${foo}, bar=${bar}`
33+
, "|FOO|BAR|"
34+
);
35+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"author": "customcommander <hello@spinjs.com>",
1212
"license": "MIT",
1313
"scripts": {
14-
"test": "tape src/*.test.js",
14+
"test": "tape index.test.js src/*.test.js",
1515
"doc": "make README.md"
1616
},
1717
"devDependencies": {

src/hide.js

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

24-
const {intersperse, constant} = require('./utils');
25-
26-
const hide = (strings, ...values) =>
27-
intersperse
28-
( strings
29-
, values.map
30-
( constant('xxx')
31-
)
32-
)
33-
.join('');
34-
3524
/**
3625
* Hides interpolated values
3726
*
@@ -42,4 +31,9 @@ const hide = (strings, ...values) =>
4231
* //=> "Hi xxx, you credit card number is xxx"
4332
* ```
4433
*/
45-
module.exports = hide;
34+
module.exports =
35+
(l, x, r) =>
36+
[ l
37+
, 'xxx'
38+
, r
39+
];

src/hide.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const test = require('tape');
2-
const sut = require('./hide');
2+
const { hide } = require('../');
33

44
test('hide: hides all values', t => {
55
t.plan(1);
66

77
const foo = 'foo';
88
const bar = 'bar';
99

10-
t.is( sut`foo=${foo}, bar=${bar}`, "foo=xxx, bar=xxx");
10+
t.is
11+
( hide`foo=${foo}, bar=${bar}`
12+
, "foo=xxx, bar=xxx"
13+
);
1114
});

src/trim.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2019 Julien Gonzalez
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
/**
25+
* Trim interpolated values
26+
*
27+
* Trim all interpolated values if they are strings.
28+
* Non-string values are left as is.
29+
*
30+
* ```javascript
31+
* const name = ' John ';
32+
* trim`My name is ${name}!`;
33+
* //=> "My name is John!"
34+
* ```
35+
*/
36+
module.exports =
37+
(l, x, r) =>
38+
[ l
39+
, typeof x === 'string'
40+
? x.trim()
41+
: x
42+
, r
43+
]

src/trim.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const test = require('tape');
2+
const { trim } = require('../');
3+
4+
test('trim: trim all values', t => {
5+
t.plan(1);
6+
7+
const foo = ' f oo ';
8+
const bar = ' b ar ';
9+
10+
t.is
11+
( trim`foo=${foo} , bar=${bar} `
12+
, "foo=f oo , bar=b ar "
13+
);
14+
});

src/upper.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2019 Julien Gonzalez
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in all
13+
* copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
* SOFTWARE.
22+
*/
23+
24+
/**
25+
* Uppercase interpolated values
26+
*
27+
* Uppercase all interpolated values if they are strings.
28+
* Non-string values are left as is.
29+
*
30+
* ```javascript
31+
* const name = 'john';
32+
* const age = 40;
33+
* upper`My name is ${name} and I am ${age} years old`
34+
* //=> "My name is JOHN and I am 40 years old"
35+
* ```
36+
*/
37+
module.exports =
38+
(l, x, r) =>
39+
[ l
40+
, typeof x === 'string'
41+
? x.toUpperCase()
42+
: x
43+
, r
44+
];

src/upper.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const test = require('tape');
2+
const { upper } = require('../');
3+
4+
test('upper: uppercase all values', t => {
5+
t.plan(1);
6+
7+
const foo = 'foo';
8+
const bar = 'bar';
9+
10+
t.is
11+
( upper`foo=${foo}, bar=${bar}`
12+
, "foo=FOO, bar=BAR"
13+
);
14+
});

0 commit comments

Comments
 (0)