Skip to content

Commit

Permalink
Merge pull request #21 from zertosh/master
Browse files Browse the repository at this point in the history
Use native JSON and Array methods
  • Loading branch information
goto-bus-stop committed Aug 13, 2019
2 parents 72fb5a8 + 0aa3818 commit 2f97008
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
19 changes: 7 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
var json = typeof JSON !== undefined ? JSON : require('jsonify');
var map = require('array-map');
var filter = require('array-filter');
var reduce = require('array-reduce');

exports.quote = function (xs) {
return map(xs, function (s) {
return xs.map(function (s) {
if (s && typeof s === 'object') {
return s.op.replace(/(.)/g, '\\$1');
}
Expand Down Expand Up @@ -36,13 +31,13 @@ for (var i = 0; i < 4; i++) {
exports.parse = function (s, env, opts) {
var mapped = parse(s, env, opts);
if (typeof env !== 'function') return mapped;
return reduce(mapped, function (acc, s) {
return mapped.reduce(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(map(filter(xs, Boolean), function (x) {
return acc.concat(xs.filter(Boolean).map(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 @@ -54,13 +49,13 @@ function parse (s, env, opts) {
'(' + CONTROL + ')', // control chars
'(' + BAREWORD + '|' + SINGLE_QUOTE + '|' + DOUBLE_QUOTE + ')*'
].join('|'), 'g');
var match = filter(s.match(chunker), Boolean);
var match = s.match(chunker).filter(Boolean);
var commented = false;

if (!match) return [];
if (!env) env = {};
if (!opts) opts = {};
return map(match, function (s, j) {
return match.map(function (s, j) {
if (commented) {
return;
}
Expand Down Expand Up @@ -192,7 +187,7 @@ function parse (s, env, opts) {
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;
}
Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,5 @@
"url": "http://substack.net"
},
"license": "MIT",
"dependencies": {
"jsonify": "~0.0.0",
"array-filter": "~0.0.0",
"array-reduce": "~0.0.0",
"array-map": "~0.0.0"
}
"dependencies": {}
}

0 comments on commit 2f97008

Please sign in to comment.