Skip to content

Commit

Permalink
modernize and format code
Browse files Browse the repository at this point in the history
  • Loading branch information
pirxpilot committed Apr 9, 2023
1 parent 147a704 commit 5f214e6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 73 deletions.
3 changes: 1 addition & 2 deletions demo/index.js
Expand Up @@ -16,5 +16,4 @@ const PARSERS = {

getlet('https://github.blog/feed/')
.pipe(stream(PARSERS))
.on('data', ({title}) => console.log(title));

.on('data', ({ title }) => console.log(title));
23 changes: 11 additions & 12 deletions lib/parsers.js
@@ -1,9 +1,9 @@
module.exports = {
object: object,
collection: collection,
addChild: addChild,
appendToCollection: appendToCollection,
assignTo: assignTo
object,
collection,
addChild,
appendToCollection,
assignTo
};

function addItemToParent(item, name, node, parent) {
Expand All @@ -14,19 +14,19 @@ function addItemToParent(item, name, node, parent) {
}

function object(name) {
return function(node, parent) {
return (node, parent) => {
return addItemToParent({}, name, node, parent);
};
}

function collection(name) {
return function(node, parent) {
return (node, parent) => {
return addItemToParent([], name, node, parent);
};
}

function addChild(name) {
return function(node, parent) {
return (node, parent) => {
if (name in parent) {
return parent[name];
}
Expand All @@ -35,8 +35,8 @@ function addChild(name) {
}

function appendToCollection(name) {
return function(node, parent) {
var child = {};
return (node, parent) => {
const child = {};

if (!parent[name]) {
parent[name] = [];
Expand All @@ -47,8 +47,7 @@ function appendToCollection(name) {
}

function assignTo(property) {
return function (text, obj) {
return (text, obj) => {
obj[property] = text;
};
}

12 changes: 6 additions & 6 deletions lib/stack.js
@@ -1,30 +1,30 @@
module.exports = stack;

function stack(init) {
var items = [];
var names = [];
const items = [];
const names = [];

if (init !== undefined) {
items.push(init);
}

return {
pop: function(name) {
pop(name) {
if (names[names.length - 1] === name) {
names.pop();
return items.pop();
}
},
push: function(item, name) {
push(item, name) {
names.push(name);
return items.push(item);
},
top: function(index) {
top(index) {
if (index === undefined) {
index = 0;
}
return items[items.length - (index + 1)];
},
empty: function() { return !items.length; }
empty() { return !items.length; }
};
}
102 changes: 49 additions & 53 deletions lib/stream.js
@@ -1,33 +1,30 @@
var Transform = require('stream').Transform;
var StringDecoder = require('string_decoder').StringDecoder;
var sax = require('sax');
var debug = require('debug')('sax-super-stream');
const { Transform } = require('stream');
const { StringDecoder } = require('string_decoder');
const sax = require('sax');
const debug = require('debug')('sax-super-stream');

var stack = require('./stack');
const stack = require('./stack');

module.exports = initParser;

// used to mark all tags that we want to skip
var IGNORE = Object.create(null);

const IGNORE = Object.create(null);

function stripPrefix(name) {
var index = name.indexOf(':');
const index = name.indexOf(':');
return index < 0 ? name : name.slice(index + 1);
}


function handlers(parserConfig, fn) {
var
items = stack(),
parsers = stack(parserConfig),
context = {},
cdata;
const items = stack();
const parsers = stack(parserConfig);
const context = {};
let cdata;

function onopentag(node) {
var tag = node.local;
var tagParser = verifyNS(parsers.top()[tag]);
var elem;
const tag = node.local;
const tagParser = verifyNS(parsers.top()[tag]);
let elem;

function parseWith(tp) {
elem = tp.call(items, node, items.top(), context);
Expand Down Expand Up @@ -65,7 +62,8 @@ function handlers(parserConfig, fn) {
}

function onclosetag(tag) {
var parser, top;
let parser;
let top;

tag = stripPrefix(tag);

Expand All @@ -85,14 +83,14 @@ function handlers(parserConfig, fn) {
}

function ontext(value) {
var textParser = parsers.top().$text;
const textParser = parsers.top().$text;
if (textParser) {
textParser.call(items, value, items.top(), context);
}
}

function onopencdata() {
var textParser = parsers.top().$text;
const textParser = parsers.top().$text;
if (textParser) {
cdata = [];
}
Expand All @@ -108,7 +106,7 @@ function handlers(parserConfig, fn) {
if (!cdata) {
return;
}
var textParser = parsers.top().$text;
const textParser = parsers.top().$text;
textParser.call(items, cdata.join(''), items.top(), context);
cdata = undefined;
}
Expand All @@ -121,13 +119,13 @@ function handlers(parserConfig, fn) {
}

return {
onopentag: onopentag,
onclosetag: onclosetag,
ontext: ontext,
onopencdata: onopencdata,
oncdata: oncdata,
onclosecdata: onclosecdata,
onerror: onerror
onopentag,
onclosetag,
ontext,
onopencdata,
oncdata,
onclosecdata,
onerror
};
}

Expand All @@ -144,13 +142,12 @@ function initParser(parserConfig, saxOptions) {
noscript: true
}, saxOptions);

var parser = sax.parser(true, saxOptions);
var decoder = new StringDecoder('utf8');
var results = [];
var parserError;
var ts;
const parser = sax.parser(true, saxOptions);
const decoder = new StringDecoder('utf8');
let results = [];
let parserError;

Object.assign(parser, handlers(parserConfig, function(err, obj) {
Object.assign(parser, handlers(parserConfig, (err, obj) => {
if (!err) {
results.push(obj);
} else {
Expand All @@ -159,32 +156,17 @@ function initParser(parserConfig, saxOptions) {
}
}));

function write(chunk) {
var str = decoder.write(chunk);
parser.write(str);
}

function flush(stream) {
if (!results.length) {
return;
}
results.forEach(function(r) {
stream.push(r);
});
results = [];
}

ts = new Transform({
const ts = new Transform({
readableObjectMode: true,
flush: function(next) {
flush(next) {
parser.close();
if (parserError) {
return next(parserError);
}
flush(this);
next();
},
transform: function(chunk, encoding, next) {
transform(chunk, encoding, next) {
if (parserError) {
return next(parserError);
}
Expand All @@ -194,5 +176,19 @@ function initParser(parserConfig, saxOptions) {
}
});
return ts;
}

function write(chunk) {
const str = decoder.write(chunk);
parser.write(str);
}

function flush(stream) {
if (!results.length) {
return;
}
results.forEach(r => {
stream.push(r);
});
results = [];
}
}

0 comments on commit 5f214e6

Please sign in to comment.