Skip to content

Commit ec7da56

Browse files
author
Your Name
committed
support stdin as input in cli
1 parent 0a0b832 commit ec7da56

File tree

7 files changed

+101
-20
lines changed

7 files changed

+101
-20
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ npm install transliteration -g
6565

6666
transliterate 你好 # Ni Hao
6767
slugify 你好 # ni-hao
68+
echo 你好 | slugify -S # ni-hao
6869
```
6970

7071
### ReactNative
@@ -200,6 +201,7 @@ Options:
200201
-u, --unknown Placeholder for unknown characters [string] [default: "[?]"]
201202
-r, --replace Custom string replacement [array] [default: []]
202203
-i, --ignore String list to ignore [array] [default: []]
204+
-S, --stdin Use stdin as input [boolean] [default: false]
203205
-h, --help Show help [boolean]
204206
205207
Examples:
@@ -221,6 +223,7 @@ Options:
221223
-s, --separator Separator of the slug [string] [default: "-"]
222224
-r, --replace Custom string replacement [array] [default: []]
223225
-i, --ignore String list to ignore [array] [default: []]
226+
-S, --stdin Use stdin as input [boolean] [default: false]
224227
-h, --help Show help [boolean]
225228
226229
Examples:

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "transliteration",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"homepage": "https://github.com/andyhu/node-transliteration",
55
"authors": [
66
"Andy Hu"

lib/bin/slugify

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1515

1616
// eslint-disable-line import/no-unresolved
1717

18+
var STDIN_ENCODING = 'utf-8'; // eslint-disable-line import/no-unresolved
19+
1820
var options = {
1921
lowercase: true,
2022
separator: '-',
2123
replace: [],
2224
ignore: []
23-
}; // eslint-disable-line import/no-unresolved
24-
25+
};
2526

2627
var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').option('l', {
2728
alias: 'lowercase',
@@ -43,6 +44,11 @@ var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').opti
4344
default: options.ignore,
4445
describe: 'String list to ignore',
4546
type: 'array'
47+
}).option('S', {
48+
alias: 'stdin',
49+
default: false,
50+
describe: 'Use stdin as input',
51+
type: 'boolean'
4652
}).help('h').option('h', {
4753
alias: 'help'
4854
}).example('$0 "你好, world!" -r 好=good -r "world=Shi Jie"', 'Replace `,` into `!` and `world` into `shijie`.\nResult: ni-good-shi-jie').example('$0 "你好,世界!" -i 你好 -i ,', 'Ignore `你好` and `,`.\nResult: 你好,shi-jie').wrap(100).argv;
@@ -81,7 +87,21 @@ if (argv.replace.length) {
8187
}
8288
}
8389
options.ignore = argv.ignore;
84-
if (argv._.length !== 1) {
85-
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
86-
}
87-
console.log((0, _slugify2.default)(argv._[0], options));
90+
91+
if (argv.stdin) {
92+
process.stdin.setEncoding(STDIN_ENCODING);
93+
process.stdin.on('readable', function () {
94+
var chunk = process.stdin.read();
95+
if (chunk !== null) {
96+
process.stdout.write((0, _slugify2.default)(chunk, options));
97+
}
98+
});
99+
process.stdin.on('end', function () {
100+
return console.log('');
101+
});
102+
} else {
103+
if (argv._.length !== 1) {
104+
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
105+
}
106+
console.log((0, _slugify2.default)(argv._[0], options));
107+
}

lib/bin/transliterate

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
1313

1414
// eslint-disable-line import/no-unresolved
1515

16+
var STDIN_ENCODING = 'utf-8'; // eslint-disable-line import/no-unresolved
17+
1618
var options = {
1719
unknown: '[?]',
1820
replace: [],
1921
ignore: []
20-
}; // eslint-disable-line import/no-unresolved
21-
22+
};
2223

2324
var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').option('u', {
2425
alias: 'unknown',
@@ -35,6 +36,11 @@ var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').opti
3536
default: options.ignore,
3637
describe: 'String list to ignore',
3738
type: 'array'
39+
}).option('S', {
40+
alias: 'stdin',
41+
default: false,
42+
describe: 'Use stdin as input',
43+
type: 'boolean'
3844
}).help('h').option('h', {
3945
alias: 'help'
4046
}).example('$0 "你好, world!" -r 好=good -r "world=Shi Jie"', 'Replace `,` into `!`, `world` into `shijie`.\nResult: Ni good, Shi Jie!').example('$0 "你好,世界!" -i 你好 -i ,', 'Ignore `你好` and `,`.\nResult: 你好,Shi Jie !').wrap(100).argv;
@@ -72,7 +78,21 @@ if (argv.replace.length) {
7278
}
7379
}
7480
options.ignore = argv.ignore;
75-
if (argv._.length !== 1) {
76-
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
77-
}
78-
console.log((0, _node.transliterate)(argv._[0], options));
81+
82+
if (argv.stdin) {
83+
process.stdin.setEncoding(STDIN_ENCODING);
84+
process.stdin.on('readable', function () {
85+
var chunk = process.stdin.read();
86+
if (chunk !== null) {
87+
process.stdout.write((0, _node.transliterate)(chunk, options));
88+
}
89+
});
90+
process.stdin.on('end', function () {
91+
return console.log('');
92+
});
93+
} else {
94+
if (argv._.length !== 1) {
95+
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
96+
}
97+
console.log((0, _node.transliterate)(argv._[0], options));
98+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "transliteration",
3-
"version": "1.2.4",
3+
"version": "1.3.0",
44
"description": "Unicode to ACSII transliteration / slugify module for node.js, browser, Web Worker, ReactNative and CLI.",
55
"main": "lib/node/index.js",
66
"scripts": {

src/bin/slugify.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import yargs from 'yargs';
33
import { parseCmdEqualOption as parseE } from '../../lib/node/utils'; // eslint-disable-line import/no-unresolved
44
import { default as slugify } from '../../lib/node/slugify'; // eslint-disable-line import/no-unresolved
55

6+
const STDIN_ENCODING = 'utf-8';
67
const options = {
78
lowercase: true,
89
separator: '-',
@@ -37,6 +38,12 @@ const argv = yargs
3738
describe: 'String list to ignore',
3839
type: 'array',
3940
})
41+
.option('S', {
42+
alias: 'stdin',
43+
default: false,
44+
describe: 'Use stdin as input',
45+
type: 'boolean',
46+
})
4047
.help('h')
4148
.option('h', {
4249
alias: 'help',
@@ -61,7 +68,19 @@ if (argv.replace.length) {
6168
}
6269
}
6370
options.ignore = argv.ignore;
64-
if (argv._.length !== 1) {
65-
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);
71+
72+
if (argv.stdin) {
73+
process.stdin.setEncoding(STDIN_ENCODING);
74+
process.stdin.on('readable', () => {
75+
const chunk = process.stdin.read();
76+
if (chunk !== null) {
77+
process.stdout.write(slugify(chunk, options));
78+
}
79+
});
80+
process.stdin.on('end', () => console.log(''));
81+
} else {
82+
if (argv._.length !== 1) {
83+
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);
84+
}
85+
console.log(slugify(argv._[0], options));
6686
}
67-
console.log(slugify(argv._[0], options));

src/bin/transliterate.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import yargs from 'yargs';
33
import { parseCmdEqualOption as parseE } from '../../lib/node/utils'; // eslint-disable-line import/no-unresolved
44
import { transliterate as tr } from '../../lib/node'; // eslint-disable-line import/no-unresolved
55

6+
const STDIN_ENCODING = 'utf-8';
67
const options = {
78
unknown: '[?]',
89
replace: [],
@@ -30,6 +31,12 @@ const argv = yargs
3031
describe: 'String list to ignore',
3132
type: 'array',
3233
})
34+
.option('S', {
35+
alias: 'stdin',
36+
default: false,
37+
describe: 'Use stdin as input',
38+
type: 'boolean',
39+
})
3340
.help('h')
3441
.option('h', {
3542
alias: 'help',
@@ -53,7 +60,19 @@ if (argv.replace.length) {
5360
}
5461
}
5562
options.ignore = argv.ignore;
56-
if (argv._.length !== 1) {
57-
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);
63+
64+
if (argv.stdin) {
65+
process.stdin.setEncoding(STDIN_ENCODING);
66+
process.stdin.on('readable', () => {
67+
const chunk = process.stdin.read();
68+
if (chunk !== null) {
69+
process.stdout.write(tr(chunk, options));
70+
}
71+
});
72+
process.stdin.on('end', () => console.log(''));
73+
} else {
74+
if (argv._.length !== 1) {
75+
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);
76+
}
77+
console.log(tr(argv._[0], options));
5878
}
59-
console.log(tr(argv._[0], options));

0 commit comments

Comments
 (0)