split-cmd
Advanced tools
Comparing version 1.0.1 to 1.1.0
56
index.js
@@ -1,55 +0,1 @@ | ||
/** | ||
* Split a command into an array. | ||
* | ||
* @example | ||
* ```js | ||
* var arr = split( 'git commit -m "some message with spaces"' ); | ||
* console.log( arr ); // [ "git", "commit", "-m", "some message with spaces" ] | ||
* ``` | ||
* | ||
* @param {string} command Command to split. | ||
* @returns {Array} | ||
*/ | ||
function split( command ) { | ||
if ( typeof command !== 'string' ) { | ||
throw new Error( 'Command must be a string' ); | ||
} | ||
var r = command.match( /[^"\s]+|"(?:\\"|[^"])*"/g ); | ||
if ( ! r ) { | ||
return []; | ||
} | ||
return r.map( function ( expr ) { | ||
var isQuoted = expr.charAt( 0 ) === '"' && expr.charAt( expr.length - 1 ) === '"'; | ||
return isQuoted ? expr.slice( 1, -1 ) : expr; | ||
} ); | ||
} | ||
/** | ||
* Split a command into an object with the attributes `command` and `args`. | ||
* | ||
* @example | ||
* ```js | ||
* var obj = splitToObject( 'git commit -m "some message with spaces"' ); | ||
* console.log( obj.command ); // git | ||
* console.log( obj.args ); // [ "commit", "-m", "some message with spaces" ] | ||
* ``` | ||
* | ||
* @param {string} command Command to split. | ||
* @returns {object} | ||
*/ | ||
function splitToObject( command ) { | ||
var cmds = split( command ); | ||
switch( cmds.length ) { | ||
case 0: return {}; | ||
case 1: return { command: cmds[ 0 ] }; | ||
default: { | ||
var first = cmds[ 0 ]; | ||
cmds.shift(); | ||
return { command: first, args: cmds }; | ||
} | ||
} | ||
} | ||
module.exports = { split, splitToObject }; | ||
function t(t){if("string"!=typeof t)throw new Error("Command must be a string");var r=t.match(/[^"\s]+|"(?:\\"|[^"])*"/g);return r?r.map(function(t){return'"'===t.charAt(0)&&'"'===t.charAt(t.length-1)?t.slice(1,-1):t}):[]}exports.split=t,exports.splitToObject=function(r){var n=t(r);switch(n.length){case 0:return{};case 1:return{command:n[0]};default:var e=n[0];return n.shift(),{command:e,args:n}}}; |
{ | ||
"name": "split-cmd", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "💦Split a command into an array or an object", | ||
"license": "MIT", | ||
"homepage": "https://github.com/thiagodp/split-cmd#readme", | ||
"bugs": { | ||
"url": "https://github.com/thiagodp/split-cmd/issues" | ||
}, | ||
"files": [ | ||
"index.*" | ||
], | ||
"source": "index.ts", | ||
"main": "index.js", | ||
"browser": "index.umd.js", | ||
"unpkg": "index.umd.js", | ||
"module": "index.esm.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"test": "jest", | ||
"build": "microbundle --no-sourcemap", | ||
"all": "pnpm run test && pnpm run build", | ||
"preversion": "jest && pnpm run build", | ||
"postversion": "echo \"Don't forget to push the tags and publish the version.\"" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"jest": "^25.1.0" | ||
"jest": "^29.7.0", | ||
"microbundle": "^0.15.1" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/thiagodp/split-cmd.git" | ||
}, | ||
"keywords": [ | ||
@@ -28,11 +42,10 @@ "split", | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/thiagodp/split-cmd.git" | ||
}, | ||
"author": { | ||
"name": "Thiago Delgado Pinto", | ||
"email": "thiago-dp@bol.com.br" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/thiagodp/split-cmd/issues" | ||
}, | ||
"homepage": "https://github.com/thiagodp/split-cmd#readme" | ||
"email": "thiago_dp@yahoo.com.br" | ||
} | ||
} |
@@ -0,1 +1,6 @@ | ||
[![npm (tag)](https://img.shields.io/npm/v/split-cmd?color=green&label=NPM&style=for-the-badge)](https://github.com/thiagodp/split-cmd/releases) | ||
[![Build Status](https://img.shields.io/github/actions/workflow/status/thiagodp/split-cmd/test.yml?style=for-the-badge)](https://github.com/thiagodp/split-cmd/actions) | ||
[![License](https://img.shields.io/npm/l/split-cmd.svg?style=for-the-badge&color=green)](https://github.com/thiagodp/split-cmd/blob/master/LICENSE.txt) | ||
[![npm](https://img.shields.io/npm/dt/split-cmd?style=for-the-badge&color=green)](https://www.npmjs.com/package/split-cmd) | ||
# split-cmd | ||
@@ -5,11 +10,15 @@ | ||
Useful for splitting a command to use with NodeJS' [child process](https://nodejs.org/api/child_process.html) methods, like [spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options), [exec](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback), and [execFile](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback), as well with libs like [execa](https://github.com/sindresorhus/execa). No external dependencies. | ||
Useful for splitting a command to use with NodeJS' [child process](https://nodejs.org/api/child_process.html) methods, like [spawn](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options), [exec](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback), and [execFile](https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback), as well with libs like [execa](https://github.com/sindresorhus/execa). | ||
## Install | ||
⚡ Just 0.4 KB uncompressed, no external dependencies. | ||
🎯 Version `1.1`+ works with NodeJS, DenoJS and browsers. JavaScript and TypeScript. | ||
## Installation | ||
```bash | ||
npm install --save split-cmd | ||
npm i split-cmd | ||
``` | ||
## Examples | ||
## Usage | ||
@@ -19,4 +28,6 @@ ### Splitting into an array | ||
```js | ||
var split = require( 'split-cmd' ).split; | ||
var arr = split( 'git commit -m "some message with spaces"' ); | ||
import { split } from 'split-cmd'; | ||
const arr = split( 'git commit -m "some message with spaces"' ); | ||
console.log( arr ); // [ "git", "commit", "-m", "some message with spaces" ] | ||
@@ -28,4 +39,6 @@ ``` | ||
```js | ||
var splitToObject = require( 'split-cmd' ).splitToObject; | ||
var obj = splitToObject( 'git commit -m "some message with spaces"' ); | ||
import { splitToObject } from 'split-cmd'; | ||
const obj = splitToObject( 'git commit -m "some message with spaces"' ); | ||
console.log( obj.command ); // git | ||
@@ -38,4 +51,4 @@ console.log( obj.args ); // [ "commit", "-m", "some message with spaces" ] | ||
```js | ||
const { splitToObject } = require( 'split-cmd' ); | ||
const execa = require( 'execa' ); | ||
import { splitToObject } from 'split-cmd'; | ||
import { execa } from 'execa'; | ||
@@ -80,3 +93,3 @@ // Executing a single command | ||
*/ | ||
splitToObject( command: string ): object | ||
splitToObject( command: string ): { command?: string, args?: string[] } | ||
``` | ||
@@ -86,2 +99,2 @@ | ||
MIT © [Thiago Delgado Pinto](https://github.com/thiagodp) | ||
MIT © [Thiago Delgado Pinto](https://github.com/thiagodp) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9
95
9541
2
89
1