Skip to content

Commit

Permalink
support stdin as input in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jan 31, 2017
1 parent 0a0b832 commit ec7da56
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ npm install transliteration -g

transliterate 你好 # Ni Hao
slugify 你好 # ni-hao
echo 你好 | slugify -S # ni-hao
```

### ReactNative
Expand Down Expand Up @@ -200,6 +201,7 @@ Options:
-u, --unknown Placeholder for unknown characters [string] [default: "[?]"]
-r, --replace Custom string replacement [array] [default: []]
-i, --ignore String list to ignore [array] [default: []]
-S, --stdin Use stdin as input [boolean] [default: false]
-h, --help Show help [boolean]
Examples:
Expand All @@ -221,6 +223,7 @@ Options:
-s, --separator Separator of the slug [string] [default: "-"]
-r, --replace Custom string replacement [array] [default: []]
-i, --ignore String list to ignore [array] [default: []]
-S, --stdin Use stdin as input [boolean] [default: false]
-h, --help Show help [boolean]
Examples:
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "transliteration",
"version": "1.2.4",
"version": "1.3.0",
"homepage": "https://github.com/andyhu/node-transliteration",
"authors": [
"Andy Hu"
Expand Down
32 changes: 26 additions & 6 deletions lib/bin/slugify
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

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

var STDIN_ENCODING = 'utf-8'; // eslint-disable-line import/no-unresolved

var options = {
lowercase: true,
separator: '-',
replace: [],
ignore: []
}; // eslint-disable-line import/no-unresolved

};

var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').option('l', {
alias: 'lowercase',
Expand All @@ -43,6 +44,11 @@ var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').opti
default: options.ignore,
describe: 'String list to ignore',
type: 'array'
}).option('S', {
alias: 'stdin',
default: false,
describe: 'Use stdin as input',
type: 'boolean'
}).help('h').option('h', {
alias: 'help'
}).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;
Expand Down Expand Up @@ -81,7 +87,21 @@ if (argv.replace.length) {
}
}
options.ignore = argv.ignore;
if (argv._.length !== 1) {
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
}
console.log((0, _slugify2.default)(argv._[0], options));

if (argv.stdin) {
process.stdin.setEncoding(STDIN_ENCODING);
process.stdin.on('readable', function () {
var chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write((0, _slugify2.default)(chunk, options));
}
});
process.stdin.on('end', function () {
return console.log('');
});
} else {
if (argv._.length !== 1) {
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
}
console.log((0, _slugify2.default)(argv._[0], options));
}
32 changes: 26 additions & 6 deletions lib/bin/transliterate
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de

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

var STDIN_ENCODING = 'utf-8'; // eslint-disable-line import/no-unresolved

var options = {
unknown: '[?]',
replace: [],
ignore: []
}; // eslint-disable-line import/no-unresolved

};

var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').option('u', {
alias: 'unknown',
Expand All @@ -35,6 +36,11 @@ var argv = _yargs2.default.version().usage('Usage: $0 <unicode> [options]').opti
default: options.ignore,
describe: 'String list to ignore',
type: 'array'
}).option('S', {
alias: 'stdin',
default: false,
describe: 'Use stdin as input',
type: 'boolean'
}).help('h').option('h', {
alias: 'help'
}).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;
Expand Down Expand Up @@ -72,7 +78,21 @@ if (argv.replace.length) {
}
}
options.ignore = argv.ignore;
if (argv._.length !== 1) {
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
}
console.log((0, _node.transliterate)(argv._[0], options));

if (argv.stdin) {
process.stdin.setEncoding(STDIN_ENCODING);
process.stdin.on('readable', function () {
var chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write((0, _node.transliterate)(chunk, options));
}
});
process.stdin.on('end', function () {
return console.log('');
});
} else {
if (argv._.length !== 1) {
console.error('Invalid argument. Please type \'' + argv.$0 + ' --help\' for help.');
}
console.log((0, _node.transliterate)(argv._[0], options));
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "transliteration",
"version": "1.2.4",
"version": "1.3.0",
"description": "Unicode to ACSII transliteration / slugify module for node.js, browser, Web Worker, ReactNative and CLI.",
"main": "lib/node/index.js",
"scripts": {
Expand Down
25 changes: 22 additions & 3 deletions src/bin/slugify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import yargs from 'yargs';
import { parseCmdEqualOption as parseE } from '../../lib/node/utils'; // eslint-disable-line import/no-unresolved
import { default as slugify } from '../../lib/node/slugify'; // eslint-disable-line import/no-unresolved

const STDIN_ENCODING = 'utf-8';
const options = {
lowercase: true,
separator: '-',
Expand Down Expand Up @@ -37,6 +38,12 @@ const argv = yargs
describe: 'String list to ignore',
type: 'array',
})
.option('S', {
alias: 'stdin',
default: false,
describe: 'Use stdin as input',
type: 'boolean',
})
.help('h')
.option('h', {
alias: 'help',
Expand All @@ -61,7 +68,19 @@ if (argv.replace.length) {
}
}
options.ignore = argv.ignore;
if (argv._.length !== 1) {
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);

if (argv.stdin) {
process.stdin.setEncoding(STDIN_ENCODING);
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write(slugify(chunk, options));
}
});
process.stdin.on('end', () => console.log(''));
} else {
if (argv._.length !== 1) {
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);
}
console.log(slugify(argv._[0], options));
}
console.log(slugify(argv._[0], options));
25 changes: 22 additions & 3 deletions src/bin/transliterate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import yargs from 'yargs';
import { parseCmdEqualOption as parseE } from '../../lib/node/utils'; // eslint-disable-line import/no-unresolved
import { transliterate as tr } from '../../lib/node'; // eslint-disable-line import/no-unresolved

const STDIN_ENCODING = 'utf-8';
const options = {
unknown: '[?]',
replace: [],
Expand Down Expand Up @@ -30,6 +31,12 @@ const argv = yargs
describe: 'String list to ignore',
type: 'array',
})
.option('S', {
alias: 'stdin',
default: false,
describe: 'Use stdin as input',
type: 'boolean',
})
.help('h')
.option('h', {
alias: 'help',
Expand All @@ -53,7 +60,19 @@ if (argv.replace.length) {
}
}
options.ignore = argv.ignore;
if (argv._.length !== 1) {
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);

if (argv.stdin) {
process.stdin.setEncoding(STDIN_ENCODING);
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
process.stdout.write(tr(chunk, options));
}
});
process.stdin.on('end', () => console.log(''));
} else {
if (argv._.length !== 1) {
console.error(`Invalid argument. Please type '${argv.$0} --help' for help.`);
}
console.log(tr(argv._[0], options));
}
console.log(tr(argv._[0], options));

0 comments on commit ec7da56

Please sign in to comment.