Skip to content

Commit

Permalink
Merge pull request #1 from axdg/es-six
Browse files Browse the repository at this point in the history
ES6 rewrite
  • Loading branch information
axdg committed May 17, 2017
2 parents 8f3f10a + b209b7a commit 5dfb9a3
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 103 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016, axdg <axdg@dfant.asia>.
Copyright (c) 2016-2017, axdg <axdg@dfant.asia>.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 7 additions & 0 deletions circle.yml
@@ -0,0 +1,7 @@
machine:
node:
version: 7

test:
override:
- npm run test:renderer
121 changes: 61 additions & 60 deletions index.js
@@ -1,72 +1,65 @@
/**
* Module dependancies
* Module dependancies.
*/
var stream = require('readable-stream');
var yaml = require('js-yaml');
const stream = require('readable-stream');
const yaml = require('js-yaml');

/**
* Constructs new front-matter parser
* given delimiter(s) and an optional
* function to transform the extracted
* front-matter
*
* @param {String|Array} delims
* @param {String|Array} d
* @param {Function} [fn]
* @return {Function} new parser
*/
function parser(delims, fn) {
fn = fn || noop;
if (typeof fn !== 'function') {
throw new TypeError('fn must be a function');
function parser(d, fn = noop) {
if (typeof fn !== 'function') throw new TypeError('fn must be a function');

const isArray = Array.isArray(d);
if (!isArray && typeof d !== 'string') {
throw new TypeError('first argument must be a string or array');
} else if (!isArray) {
d = [d];
} else if (!d.length) {
throw new Error('first argument cannot be an empty array');
}

var arr = Array.isArray(delims);
if (!arr && typeof delims !== 'string') {
throw new TypeError('delims must be a string or array');
} else if (!arr) {
delims = [delims];
} else if (!delims.length) {
throw new Error('delims cannot be an empty array');
}

// compile opening `o` and closing `c` RegExps
var o = new RegExp('^\uFEFF?' + escape(delims[0]) + '\r?\n');
var c = new RegExp('\n' + escape(delims[1] || delims[0]) + '(?:\r?\n|$)');
// Compile opening (`o`) and closing (`c`) RegExps.
const o = new RegExp('^\uFEFF?' + escape(d[0]) + '\r?\n');
const c = new RegExp('\n' + escape(d[1] || d[0]) + '(?:\r?\n|$)');

/**
* Test a string to determine
* if it contains front-matter
* if it contains front-matter.
*
* @param {String} str
* @return {Boolean} front-matter?
* @param {String}
* @return {Boolean}
*/
var _this = function (str) {
var out = {
const _this = function (str = '') {
const out = {
attributes: null,
body: str,
};

if (!o.test(str)) {
return out;
}
if (!o.test(str)) return out;

var end = str.search(c);
const end = str.search(c);

if (end === -1) {
return out;
}
if (end === -1) return out;

// slice out and parse the front-matter
// Slice out and parse the front-matter.
out.attributes = fn(str.slice(str.indexOf('\n') + 1, end));

// check if the body actually exists
var index = (str.indexOf('\n', end + 1) + 1);
// Check if the body actually exists.
const index = (str.indexOf('\n', end + 1) + 1);
if (!index) {
out.body = '';
return out;
}

// slice out the body
// Slice out the body.
out.body = str.slice(str.indexOf('\n', end + 1) + 1);
return out;
};
Expand All @@ -81,49 +74,57 @@ function parser(delims, fn) {
* @api private
*/
_this.through = function () {
var self = this; // actually _this
var data = '';
var inBody = false;

// this could still be optimized
// for where fron-matter doesn't exist
// if no opening delim is encountered
// the stream should begin emitting right away
const self = this; // Actually `_this` :)

let data = '';
let inDocumentBody = false;

/**
* NOTE: This could still be optimized
* for where fron-matter doesn't exist
* if no opening delim is encountered
* the stream should begin emitting right away.
*/
return new stream.Transform({
transform: function (chunk, encoding, next) {
if (!inBody) {
if (!inDocumentBody) {
data += String(chunk);
try {
var split = self(data);

const split = self(data);

// if front matter was captured emit the
// attributes event, push whatever remains
/**
* If front matter was captured emit the
* attributes event, push whatever remains.
*/
if (split.attributes) {
inBody = !inBody;
inDocumentBody = !inDocumentBody;
this.emit('attributes', split.attributes);
return next(null, split.body);
}
} catch (err) {
next(err);
}

// make sure str could actually
// contain a front-matter block,
// if not, no need to accumulate chunks
/**
* make sure `str` could actually
* contain a front-matter block,
* if not, no need to accumulate chunks.
*/
if (data.length > 6 && !o.test(data)) {
inBody = !inBody;
inDocumentBody = !inDocumentBody;
next(null, data);
}
} else {
next(null, chunk);
}
},
flush: function (done) {
// worst case scenario; str began with an
// opening delim, but the closing delim
// does not exist, push all data to buffer
if (!inBody) {
/**
* worst case scenario; `str` began with an
* opening delim, but the closing delim
* does not exist, push all data to buffer
*/
if (!inDocumentBody) {
this.emit('attributes', null);
this.push(data);
}
Expand All @@ -147,7 +148,7 @@ function parser(delims, fn) {
}

/**
* Escape user supplied delimiter
* Escape user supplied delimiter.
*
* @param {String} str
* @return {String} RegExp escaped string
Expand All @@ -167,7 +168,7 @@ function noop(str) {
return str;
}

// parser is the default export
// The parser is the default export.
module.exports = parser;

// expose yaml and json front matter
Expand Down

0 comments on commit 5dfb9a3

Please sign in to comment.