Skip to content

Commit

Permalink
es5 shims
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Dec 24, 2013
1 parent 3fc22d3 commit 00dc6ab
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 46 deletions.
42 changes: 35 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var json = typeof JSON !== undefined ? JSON : require('jsonify');

exports.quote = function (xs) {
return xs.map(function (s) {
return map(xs, function (s) {
if (s && typeof s === 'object') {
return s.op.replace(/(.)/g, '\\$1');
}
Expand Down Expand Up @@ -31,13 +33,13 @@ for (var i = 0; i < 4; i++) {
exports.parse = function (s, env) {
var mapped = parse(s, env);
if (typeof env !== 'function') return mapped;
return mapped.reduce(function (acc, s) {
return reduce(mapped, function (acc, s) {
if (typeof s === 'object') return acc.concat(s);
var xs = s.split(RegExp('(' + TOKEN + '.*?' + TOKEN + ')', 'g'));
if (xs.length === 1) return acc.concat(xs[0]);
return acc.concat(xs.filter(Boolean).map(function (x) {
return acc.concat(map(filter(xs, Boolean), function (x) {
if (RegExp('^' + TOKEN).test(x)) {
return JSON.parse(x.split(TOKEN)[1]);
return json.parse(x.split(TOKEN)[1]);
}
else return x;
}));
Expand All @@ -49,11 +51,11 @@ function parse (s, env) {
'(' + CONTROL + ')', // control chars
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
].join('|'), 'g');
var match = s.match(chunker).filter(Boolean);
var match = filter(s.match(chunker), Boolean);

if (!match) return [];
if (!env) env = {};
return match.map(function (s) {
return map(match, function (s) {
if (RegExp('^' + CONTROL + '$').test(s)) {
return { op: s };
}
Expand Down Expand Up @@ -169,8 +171,34 @@ function parse (s, env) {
if (r === undefined) r = '';

if (typeof r === 'object') {
return pre + TOKEN + JSON.stringify(r) + TOKEN;
return pre + TOKEN + json.stringify(r) + TOKEN;
}
else return pre + r;
}
};

function filter (xs, f) {
if (xs.filter) return xs.filter(f);
var res = [];
for (var i = 0; i < xs.length; i++) {
if (f(xs[i], i)) res.push(xs[i]);
}
return res;
}

function map (xs, f) {
if (xs.map) return xs.map(f);
var res = [];
for (var i = 0; i < xs.length; i++) {
res.push(f(xs[i], i));
}
return res;
}

function reduce (xs, f, acc) {
if (xs.reduce) return xs.reduce(f, acc);
for (var i = 0; i < xs.length; i++) {
acc = f(acc, xs[i], i);
}
return acc;
}
85 changes: 46 additions & 39 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,48 @@
{
"name" : "shell-quote",
"version" : "1.4.0",
"description" : "quote and parse shell commands",
"main" : "index.js",
"devDependencies" : {
"tape" : "~2.3.0"
},
"scripts" : {
"test" : "tape test/*.js"
},
"repository" : {
"type" : "git",
"url" : "http://github.com/substack/node-shell-quote.git"
},
"keywords" : [
"shell",
"command",
"quote",
"parse"
],
"testling": {
"files": "test/*.js",
"browsers": [
"ie/6..latest",
"firefox/3.5", "firefox/15..latest", "firefox/nightly",
"chrome/25..latest", "chrome/canary",
"opera/10..latest", "opera/next",
"safari/5.1..latest",
"ipad/6.0..latest",
"iphone/6.0..latest",
"android-browser/4.2..latest"
]
},
"author" : {
"name" : "James Halliday",
"email" : "mail@substack.net",
"url" : "http://substack.net"
},
"license" : "MIT"
"name": "shell-quote",
"version": "1.4.0",
"description": "quote and parse shell commands",
"main": "index.js",
"devDependencies": {
"tape": "~2.3.0"
},
"scripts": {
"test": "tape test/*.js"
},
"repository": {
"type": "git",
"url": "http://github.com/substack/node-shell-quote.git"
},
"keywords": [
"shell",
"command",
"quote",
"parse"
],
"testling": {
"files": "test/*.js",
"browsers": [
"ie/6..latest",
"firefox/3.5",
"firefox/15..latest",
"firefox/nightly",
"chrome/25..latest",
"chrome/canary",
"opera/10..latest",
"opera/next",
"safari/5.1..latest",
"ipad/6.0..latest",
"iphone/6.0..latest",
"android-browser/4.2..latest"
]
},
"author": {
"name": "James Halliday",
"email": "mail@substack.net",
"url": "http://substack.net"
},
"license": "MIT",
"dependencies": {
"jsonify": "0.0.0"
}
}

0 comments on commit 00dc6ab

Please sign in to comment.