<commit_before>const _ = require('underscore');
const acquit = require('acquit');
const fs = require('fs');
const markdown = require('marked');
const jade = require('jade');
const file = require('file');
const feed = require('feed');
const transform = require('acquit-require');

require('acquit-ignore')();

const highlight = require('highlight.js');
markdown.setOptions({
  highlight: function(code) {
    return highlight.highlight('JavaScript', code).value;
  }
});

const wagner = require('wagner-core');

const posts = require('./lib/posts');
const postsConfig = _.sortBy(posts, function(post) { return -post.date.unix(); });

const templates = {
  index: null,
  post: null,
  list: null
};
const postsContent = {};
const postBySource = {};

const loadTemplate = function(path, key) {
  return asyncFunctionToThunk(async function() {
    const view = await new Promise((resolve, reject) => {
      fs.readFile(path, (err, res) => {
        if (err != null) {
          return reject(err);
        }
        resolve(res);
      })
    });
    const result = jade.compile(view, { filename: path });
    return result;
  });
};

wagner.task('index', loadTemplate('./lib/views/index.jade', 'index'));
wagner.task('postTemplate', loadTemplate('./lib/views/post.jade', 'post'));
wagner.task('listTemplate', loadTemplate('./lib/views/list.jade', 'list'));

async function getPosts() {
  const tests = [
    ...acquit.parse(fs.readFileSync('./test/20180621.test.js').toString()),
    ...acquit.parse(fs.readFileSync('./test/20180702.test.js').toString()),
    ...acquit.parse(fs.readFileSync('./test/20180710.test.js').toString())
  ];

  await Promise.all(posts.map(async (post) => {
    const md = await new Promise((resolve, reject) => {
      fs.readFile(post.src, (err, res) => {
        if (err != null) {
          return reject(err);
        }
        resolve(res);
      });
    });

    post.md = md.toString();
    post.md = transform(post.md, tests);
  }));

  return posts;
}

function asyncFunctionToThunk(fn) {
  return function(callback) {
    fn().then(res => callback(null, res), err => callback(err));
  };
}

var tasks = ['loadIndex'];
var tags = {};
_.each(posts, function(post) {
  _.each(post.tags || [], function(tag) {
    tags[tag] = tags[tag] || [];
    tags[tag].unshift(post);
  });
});

wagner.task('compiledPosts', asyncFunctionToThunk(getCompiledPosts));

async function getCompiledPosts() {
  const posts = await getPosts();

  const filename = './lib/views/post.jade';
  const postTemplate = jade.compile(fs.readFileSync(filename, 'utf8'), { filename });

  for (const post of posts) {
    const output = postTemplate({
      post: post,
      content: markdown(post.md.toString()),
      allPosts: postsConfig
    });

    post.compiled = output;
    post.preview = markdown(post.md.substr(0, post.md.indexOf('\n')));
  }

  return posts;
}

wagner.task('generatePosts', asyncFunctionToThunk(async function() {
  console.log('Generating posts');
  const compiledPosts = await getCompiledPosts();

  for (const post of compiledPosts) {
    console.log('Writing "' + post.title + '"');
    const path = file.path.join(post.dest.directory, post.dest.name + '.html');
    fs.writeFileSync(path, post.compiled);
  }
}));

wagner.task('tags', function(compiledPosts, listTemplate, callback) {
  wagner.parallel(
    tags,
    function(tag, key, callback) {
      var output = listTemplate({
        tag: key,
        posts: tag,
        allPosts: postsConfig,
        pageNum: 0,
        isLastPage: false
      });

      fs.writeFile('./bin/tag/' + key.toLowerCase() + '.html', output, function(err) {
        if (err) {
          console.log('Error writing file: ' + err);
          return callback(err);
        }
        return callback(null);
      });
    }, callback);
});

wagner.task('compiledIndex', function(compiledPosts, index, callback) {
  var posts = _.map(compiledPosts, function(p) {
    return p;
  });
  var output = index({
    posts: posts.reverse(),
    allPosts: postsConfig
  });
  fs.writeFile('./bin/index.html', output, function(err) {
    if (err) {
      console.log('Error writing index: ' + err);
      return callback(err);
    }
    fs.writeFile('./bin/CNAME', 'thecodebarbarian.com', function(err) {
      if (err) {
        console.log('Error writing CNAME: ' + err);
        return callback(err);
      }
      return callback(null);
    });
  });
});

wagner.task('recommendationsTemplate',
  loadTemplate('./lib/views/recommendations.jade', 'recommendations'));
wagner.task('recommendations', function(recommendationsTemplate, callback) {
  fs.writeFile('./bin/recommendations.html', recommendationsTemplate(), function(err) {
    if (err) {
      console.log('Error writing index: ' + err);
      return callback(err);
    }
    callback();
  });
});

wagner.task('pages', function(compiledPosts, listTemplate, callback) {
  var posts = _.map(compiledPosts, function(p) {
    return p;
  });

  var pages = [];
  var reversed = posts.reverse();
  for (var i = 8; i < posts.length; i += 8) {
    pages.push(listTemplate({
      tag: 'Page ' + Math.floor(i / 8),
      posts: reversed.slice(i, i + 8),
      allPosts: postsConfig,
      pageNum: Math.floor(i / 8),
      isLastPage: !(i + 8 < posts.length)
    }));
  }

  wagner.parallel(pages, function(page, index, callback) {
    fs.writeFile('./bin/page/' + (index + 1) + '.html', page, function(err) {
      callback(err);
    });
  }, callback);
});

wagner.task('feed', function(compiledPosts, callback) {
  var posts = Array.from(compiledPosts);

  var f = new feed({
    title: 'TheCodeBarbarian.com',
    description: 'Detailed articles about the MEAN stack and related topics',
    link: 'http://thecodebarbarian.com',
    image: 'http://thecodebarbarian.com/images/Barbarian_Head.png',
    author: {
      name: 'Valeri Karpov'
    }
  });

  var reversed = posts.reverse();
  for (var i = 0; i < reversed.length; ++i) {
    f.addItem({
      title: posts[i].title,
      link: 'http://www.thecodebarbarian.com' + posts[i].dest.directory.substr('./bin'.length) + '/' + posts[i].dest.name + '.html',
      date: posts[i].date.toDate()
    });
  }

  fs.writeFile('./bin/feed.xml', f.render('rss-2.0').replace(new RegExp('<content:encoded/>', 'g'), ''), callback);
});

wagner.invokeAsync(function(error, compiledIndex, pages, generatePosts, tags, feed, recommendations) {
  if (error) {
    return console.log('Errors occurred: ' + error + '\n' + error.stack);
  }
  console.log('Done');
});

module.exports = function(cb) {
  wagner.invokeAsync(function(error, compiledIndex, generatePosts, tags, feed, recommendations) {
    if (error) {
      cb(error);
      return console.log('Errors occurred: ' + error + '\n' + error.stack);
    }
    cb();
    console.log('Done');
  });
};
<commit_msg>refactor: make all tasks async functions<commit_after>const _ = require('underscore');
const acquit = require('acquit');
const fs = require('fs');
const markdown = require('marked');
const jade = require('jade');
const file = require('file');
const feed = require('feed');
const transform = require('acquit-require');

require('acquit-ignore')();

const highlight = require('highlight.js');
markdown.setOptions({
  highlight: function(code) {
    return highlight.highlight('JavaScript', code).value;
  }
});

const wagner = require('wagner-core');

const posts = require('./lib/posts');
const postsConfig = _.sortBy(posts, function(post) { return -post.date.unix(); });

const templates = {
  index: null,
  post: null,
  list: null
};
const postsContent = {};
const postBySource = {};

const loadTemplate = function(path, key) {
  return asyncFunctionToThunk(async function() {
    const view = await new Promise((resolve, reject) => {
      fs.readFile(path, (err, res) => {
        if (err != null) {
          return reject(err);
        }
        resolve(res);
      })
    });
    const result = jade.compile(view, { filename: path });
    return result;
  });
};

wagner.task('index', loadTemplate('./lib/views/index.jade', 'index'));
wagner.task('postTemplate', loadTemplate('./lib/views/post.jade', 'post'));
wagner.task('listTemplate', loadTemplate('./lib/views/list.jade', 'list'));

async function getPosts() {
  const tests = [
    ...acquit.parse(fs.readFileSync('./test/20180621.test.js').toString()),
    ...acquit.parse(fs.readFileSync('./test/20180702.test.js').toString()),
    ...acquit.parse(fs.readFileSync('./test/20180710.test.js').toString())
  ];

  await Promise.all(posts.map(async (post) => {
    const md = await new Promise((resolve, reject) => {
      fs.readFile(post.src, (err, res) => {
        if (err != null) {
          return reject(err);
        }
        resolve(res);
      });
    });

    post.md = md.toString();
    post.md = transform(post.md, tests);
  }));

  return posts;
}

function asyncFunctionToThunk(fn) {
  return function(callback) {
    fn().then(res => callback(null, res), err => callback(err));
  };
}

var tasks = ['loadIndex'];
var tags = {};
_.each(posts, function(post) {
  _.each(post.tags || [], function(tag) {
    tags[tag] = tags[tag] || [];
    tags[tag].unshift(post);
  });
});

wagner.task('compiledPosts', asyncFunctionToThunk(getCompiledPosts));

async function getCompiledPosts() {
  const posts = await getPosts();

  const filename = './lib/views/post.jade';
  const postTemplate = jade.compile(fs.readFileSync(filename, 'utf8'), { filename });

  for (const post of posts) {
    const output = postTemplate({
      post: post,
      content: markdown(post.md.toString()),
      allPosts: postsConfig
    });

    post.compiled = output;
    post.preview = markdown(post.md.substr(0, post.md.indexOf('\n')));
  }

  return posts;
}

wagner.task('generatePosts', asyncFunctionToThunk(async function() {
  console.log('Generating posts');
  const compiledPosts = await getCompiledPosts();

  for (const post of compiledPosts) {
    console.log('Writing "' + post.title + '"');
    const path = file.path.join(post.dest.directory, post.dest.name + '.html');
    fs.writeFileSync(path, post.compiled);
  }
}));

wagner.task('tags', asyncFunctionToThunk(async function() {
  const filename = './lib/views/list.jade';
  const listTemplate = jade.compile(fs.readFileSync(filename, 'utf8'), { filename });

  for (const key of Object.keys(tags)) {
    const output = listTemplate({
      tag: key,
      posts: [].concat(tags[key]).reverse(),
      allPosts: postsConfig,
      pageNum: 0,
      isLastPage: false
    });

    fs.writeFileSync('./bin/tag/' + key.toLowerCase() + '.html', output);
  }
}));

wagner.task('compiledIndex', asyncFunctionToThunk(async function() {
  const filename = './lib/views/index.jade';
  const index = jade.compile(fs.readFileSync(filename, 'utf8'), { filename });

  const compiledPosts = await getCompiledPosts();

  const posts = [].concat(compiledPosts).reverse();
  const output = index({
    posts,
    allPosts: postsConfig
  });

  fs.writeFileSync('./bin/index.html', output);
  fs.writeFileSync('./bin/CNAME', 'thecodebarbarian.com');
}));

wagner.task('recommendations', asyncFunctionToThunk(async function() {
  const filename = './lib/views/recommendations.jade';
  const recommendations = jade.compile(fs.readFileSync(filename, 'utf8'), { filename });

  fs.writeFileSync('./bin/recommendations.html', recommendations());
}));

wagner.task('pages', asyncFunctionToThunk(async function() {
  const compiledPosts = await getCompiledPosts();
  const filename = './lib/views/list.jade';
  const listTemplate = jade.compile(fs.readFileSync(filename, 'utf8'), { filename });

  var pages = [];
  var reversed = [].concat(compiledPosts).reverse();
  for (var i = 8; i < posts.length; i += 8) {
    pages.push(listTemplate({
      tag: 'Page ' + Math.floor(i / 8),
      posts: reversed.slice(i, i + 8),
      allPosts: postsConfig,
      pageNum: Math.floor(i / 8),
      isLastPage: !(i + 8 < posts.length)
    }));
  }

  for (const [i, page] of Object.entries(pages)) {
    fs.writeFileSync('./bin/page/' + (+i + 1) + '.html', page);
  }
}));

wagner.task('feed', asyncFunctionToThunk(async function() {
  const compiledPosts = await getCompiledPosts();
  const posts = Array.from(compiledPosts);

  const f = new feed({
    title: 'TheCodeBarbarian.com',
    description: 'Detailed articles about the MEAN stack and related topics',
    link: 'http://thecodebarbarian.com',
    image: 'http://thecodebarbarian.com/images/Barbarian_Head.png',
    author: {
      name: 'Valeri Karpov'
    }
  });

  const reversed = posts.reverse();
  for (let i = 0; i < reversed.length; ++i) {
    f.addItem({
      title: posts[i].title,
      link: 'http://www.thecodebarbarian.com' + posts[i].dest.directory.substr('./bin'.length) + '/' + posts[i].dest.name + '.html',
      date: posts[i].date.toDate()
    });
  }

  fs.writeFileSync('./bin/feed.xml', f.render('rss-2.0').replace(new RegExp('<content:encoded/>', 'g'), ''));
}));

wagner.invokeAsync(function(error, compiledIndex, pages, generatePosts, tags, feed, recommendations) {
  if (error) {
    return console.log('Errors occurred: ' + error + '\n' + error.stack);
  }
  console.log('Done');
});

module.exports = function(cb) {
  wagner.invokeAsync(function(error, compiledIndex, generatePosts, tags, feed, recommendations) {
    if (error) {
      cb(error);
      return console.log('Errors occurred: ' + error + '\n' + error.stack);
    }
    cb();
    console.log('Done');
  });
};
<|endoftext|>


<commit_before>'use strict'
const {resolve} = require('path')
const packages = resolve(__dirname, 'packages')
const {defineProperty} = Object

require('fs')
  .readdirSync(packages)
  .map(item => [resolve(packages, item), item])
  .forEach(([pkgname, item]) => {
    const desc = {enumerable: true}

    try {
      desc.value = require(pkgname)
    } catch (error) {
      console.error(error)
      desc.get = () => require(pkgname)
    }

    ; [pkgname, item].forEach(
      name => defineProperty(exports, name, desc)
    )
  })
<commit_msg>Convert to camelCase<commit_after>'use strict'
const {resolve} = require('path')
const packages = resolve(__dirname, 'packages')
const {defineProperty} = Object

require('fs')
  .readdirSync(packages)
  .map(item => [resolve(packages, item), item])
  .forEach(([pkgname, item]) => {
    const desc = {enumerable: true}

    try {
      desc.value = require(pkgname)
    } catch (error) {
      console.error(error)
      desc.get = () => require(pkgname)
    }

    ; [pkgname, createCamelCaseName(item)].forEach(
      name => defineProperty(exports, name, desc)
    )
  })

function createCamelCaseName (dashes) {
  const [first, ...rest] = String(dashes).split(/[-_ ]/)
  const reststr = rest.map(capitalize).join('')
  return first + reststr
}

function capitalize ([first, ...rest]) {
  const firststr = String(first).toUpperCase()
  const reststr = rest.map(x => String(x).toLowerCase()).join('')
  return firststr + reststr
}
<|endoftext|>


<commit_before>/*eslint no-undef: "error"*/
/*eslint-env node*/

var L = require('leaflet');

require('leaflet-editable');
require('leaflet-path-transform/src/Matrix');
require('./src/Textbox');
require('./src/Util');
require('./src/Editable.Textbox');

module.exports = L.Editable.TextBoxEditor;
<commit_msg>require SVG text renderer<commit_after>/*eslint no-undef: "error"*/
/*eslint-env node*/

var L = require('leaflet');

require('leaflet-editable');
require('leaflet-path-transform/src/Matrix');
require('./src/Textbox');
require('./src/Util');
require('./src/Editable.Textbox');
require('./src/SVG');

module.exports = L.Editable.TextBoxEditor;
<|endoftext|>


<commit_before>var minimist = require('minimist');

var cmds = {};
var options = {};
var positionals = {};
var aliases = {};
var all = [];

var noop = function() {};

var unopt = function(opt) {
	return opt.replace(/-/g, '');
};

var opt = function(name) {
	return '--'+name;
};

var toFunction = function(values) {
	if (typeof values === 'function') return values;
	return function(callback) {
		callback(null, values);
	};
};

var tab = function(name) {
	if (typeof name === 'function') return tab()(name);

	var index = name ? 1 : 0;

	name = name || '__main__';
	options[name] = {};
	positionals[name] = {};

	var onpositional = function(values) {
		if (name in cmds) positionals[name][index++] = toFunction(cmds[name]);
		cmds[name] = values;
	};

	var onoption = function(long, short, values) {
		if (typeof short !== 'string') return onoption(long, long, short);
		aliases[short] = long;
		options[name][unopt(long)] = toFunction(values);
	};

	var opt = function(a, b, c) {
		if (typeof a === 'string' && a[0] === '-') onoption(a, b, c);
		else onpositional(a);
		return opt;
	};

	all.forEach(function(args) {
		opt.apply(null, args);
	});

	return opt;
};

tab.all = function() {
	all.push(arguments);
	return tab.all;
};

tab.complete = function(index, words) {
	var cur = words[index] || '';
	var prev = words[index-1] || '';
	var _ = minimist(words.slice(0, index+1))._;
	var cmd = _[0];

	var callback = function(err, values) {
		if (err) return;
		values = [].concat(values || []).filter(function(value) {
			return (''+value).slice(0, cur.length) === cur;
		});
		console.log(values.join('\n'));
	};

	if (cur.slice(0, 2) === '--') {
		var names = Object.keys(options[cmd] || options.__main__ ||  {});
		return callback(null, names.map(opt));
	}

	if (aliases[cur]) return console.log(aliases[cur]);
	if (aliases[prev]) prev = aliases[prev];

	if (prev.slice(0, 2) === '--') {
		var name = unopt(prev);
		var fn = (options[cmd] || options.__main__ || {})[name];
		return fn ? fn(callback) : callback();
	}

	if (prev[0] === '-' || cur[0] === '-') return callback();

	index = _.length-1;

	var pos = (positionals[cmd] || positionals.__main__ || {})[index];
	if (pos) return pos(callback);

	if (_.length < 2) {
		delete cmds.__main__
		return callback(null, Object.keys(cmds));
	}

	callback();
};

tab.help = function() {
	console.log('Unknown command');
};

tab.install = function() {
	require('./install'); // deferred required for faster tab completion load
};

tab.parse = function(argv) {
	argv = argv || process.argv.slice(2);

	if (argv[0] === '--tabalot') {
		if (argv.length === 1) tab.install();
		else tab.complete(Number(argv[1]), argv.slice(2));
		process.exit(0);
	}

	argv = minimist(argv);

	var apply = function(cmd) {
		var arity = Object.keys(positionals[cmd]).length;
		var args = [];

		for (var i = 0; i < arity; i++) {
			args[i] = argv._[i];
		}

		delete argv._;
		args.push(argv);
		cmds[cmd].apply(null, args);
		return true;
	};

	var cmd = argv._[0];

	if (!cmds[cmd]) {
		if (cmds.__main__) return apply('__main__');
		return false;
	}

	argv._.shift();
	return apply(cmd);
};

module.exports = tab;
<commit_msg>always routing on next tick<commit_after>var minimist = require('minimist');

var cmds = {};
var options = {};
var positionals = {};
var aliases = {};
var all = [];

var noop = function() {};

var unopt = function(opt) {
	return opt.replace(/-/g, '');
};

var opt = function(name) {
	return '--'+name;
};

var toFunction = function(values) {
	if (typeof values === 'function') return values;
	return function(callback) {
		callback(null, values);
	};
};

var tab = function(name) {
	if (typeof name === 'function') return tab()(name);

	var index = name ? 1 : 0;

	name = name || '__main__';
	options[name] = {};
	positionals[name] = {};

	var onpositional = function(values) {
		if (name in cmds) positionals[name][index++] = toFunction(cmds[name]);
		cmds[name] = values;
	};

	var onoption = function(long, short, values) {
		if (typeof short !== 'string') return onoption(long, long, short);
		aliases[short] = long;
		options[name][unopt(long)] = toFunction(values);
	};

	var opt = function(a, b, c) {
		if (typeof a === 'string' && a[0] === '-') onoption(a, b, c);
		else onpositional(a);
		return opt;
	};

	all.forEach(function(args) {
		opt.apply(null, args);
	});

	return opt;
};

tab.all = function() {
	all.push(arguments);
	return tab.all;
};

tab.complete = function(index, words) {
	var cur = words[index] || '';
	var prev = words[index-1] || '';
	var _ = minimist(words.slice(0, index+1))._;
	var cmd = _[0];

	var callback = function(err, values) {
		if (err) return;
		values = [].concat(values || []).filter(function(value) {
			return (''+value).slice(0, cur.length) === cur;
		});
		console.log(values.join('\n'));
	};

	if (cur.slice(0, 2) === '--') {
		var names = Object.keys(options[cmd] || options.__main__ ||  {});
		return callback(null, names.map(opt));
	}

	if (aliases[cur]) return console.log(aliases[cur]);
	if (aliases[prev]) prev = aliases[prev];

	if (prev.slice(0, 2) === '--') {
		var name = unopt(prev);
		var fn = (options[cmd] || options.__main__ || {})[name];
		return fn ? fn(callback) : callback();
	}

	if (prev[0] === '-' || cur[0] === '-') return callback();

	index = _.length-1;

	var pos = (positionals[cmd] || positionals.__main__ || {})[index];
	if (pos) return pos(callback);

	if (_.length < 2) {
		delete cmds.__main__
		return callback(null, Object.keys(cmds));
	}

	callback();
};

tab.help = function() {
	console.log('Unknown command');
};

tab.install = function() {
	require('./install'); // deferred required for faster tab completion load
};

tab.parse = function(argv) {
	argv = argv || process.argv.slice(2);

	if (argv[0] === '--tabalot') {
		if (argv.length === 1) tab.install();
		else tab.complete(Number(argv[1]), argv.slice(2));
		process.exit(0);
	}

	argv = minimist(argv);

	var apply = function(cmd) {
		process.nextTick(function() {
			var arity = Object.keys(positionals[cmd]).length;
			var args = [];

			for (var i = 0; i < arity; i++) {
				args[i] = argv._[i];
			}

			delete argv._;
			args.push(argv);
			cmds[cmd].apply(null, args);
		});
		return true;
	};

	var cmd = argv._[0];

	if (!cmds[cmd]) {
		if (cmds.__main__) return apply('__main__');
		return false;
	}

	argv._.shift();
	return apply(cmd);
};

module.exports = tab;
<|endoftext|>


<commit_before>module.exports = function(apiKey){
    var request = require('request');

    request = request.defaults({
        headers: {
            'x-referit-apikey' : apiKey
        }
    });

    return {
        agents: require('./agents/agents')(request)
    };
};

<commit_msg>added bounties to the index file<commit_after>module.exports = function(apiKey){
    var request = require('request');

    request = request.defaults({
        headers: {
            'x-referit-apikey' : apiKey
        }
    });

    return {
        agents: require('./agents/agents')(request),
        bounties: require('./bounties/bounties')(request)
    };
};

<|endoftext|>


<commit_before>'use strict'
const Duplex = require('readable-stream').Duplex
const Writable = require('readable-stream').Writable
const Readable = require('readable-stream').Readable

function AppError (code, message) {
  Error.apply(this, [message])
  this.code = code
}
AppError.prototype = Object.create(Error)
AppError.prototype.toString = function () {
  return '[' + this.code + '] ' + this.message
}

const createStream = function (options) {
  if (!options) {
    options = {}
  }
  const rwClean = String(options.mode).trim()
  const readable = rwClean === 'r' || rwClean === 'rw'
  const writable = rwClean === 'w' || rwClean === 'rw'
  if (!options.node) {
    throw new AppError('ERRNODE', 'Option Error: `node` is missing!')
  }
  if (!readable && !writable) {
    throw new AppError('ERRMODE', 'Option Error: `mode` needs to be `r`, `w` or `rw`!')
  }
  const buffer = options.node.child('buffer')
  const finRef = options.node.child('finished')
  var _waitFor
  if (writable) {
    _waitFor = Promise.all([
      options.node.child('started').set(new Date().toISOString()),
      buffer.set([]),
      finRef.set(null)
    ])
  } else {
    _waitFor = Promise.resolve(null)
  }
  if (options.enableTime) {
    // Enable time enforces objectMode on the stream since the times are stored
    // in an object
    options.objectMode = true
  }
  var finished = false
  var lock = false
  var count = 0
  const putNext = function (data, encoding, callback) {
    const payload = {}
    const line = {
      time: new Date().toISOString()
    }
    if (!options.binary && data instanceof Buffer) {
      data = data.toString()
      encoding = 'utf8'
    }
    if (data !== undefined) {
      line.data = data
    }
    if (encoding !== undefined) {
      line.encoding = encoding
    }
    payload[count] = line
    count += 1
    _waitFor.then(function () {
      return buffer.update(payload)
    }).then(function () {
      if (data === null) {
        return finRef.set(new Date().toISOString()).then(function () {
          return new Promise(function (resolve) {
            setImmediate(resolve)
          })
        })
      }
      return null
    }).then(function () {
      if (readable && !lock) {
        // Duplex stream processing
        if (data === null) {
          if (options.enableTime) {
            stream.push(line)
          }
          stream.push(null)
          finish()
        } else if (options.enableTime) {
          stream.push(line)
        } else if (options.objectMode || data === undefined || typeof data === 'string' || data instanceof Buffer) {
          stream.push(data, options.objectMode ? undefined : encoding)
        } else {
          stream.push(String(data))
        }
      }
      if (callback) {
        callback()
      }
    }).catch(function (err) {
      if (callback) {
        callback(err)
      }
      console.log('WARNING: Error in stream')
      console.log(err.stack || err)
    })
  }
  const finish = function () {
    if (!finished) {
      finished = true
      buffer.off('child_removed', removeCheck)
      buffer.off('child_added', onData)
      return true
    }
    return false
  }
  const stream = readable ? (writable ? new Duplex(options) : new Readable(options)) : new Writable(options)
  const onData = function (snap) {
    var raw = snap.val()
    if (raw === null) {
      // This case should never occur. But if it does through manual manipulation
      // it will not break anything
      return
    }
    if (raw.data === undefined) {
      raw.data = null
    } else if (!(
      raw.data === null ||
      typeof raw.data === 'string' ||
      raw.data instanceof Buffer
    )) {
      if (raw.data.type === 'Buffer') {
        raw.data = Buffer.from(raw.data.data)
      } else if (!options.objectMode) {
        // Make sure that we stringify the data
        raw.data = String(raw.data)
      }
    }
    if (raw.data === null) {
      if (finish()) {
        const waitForFinish = function (snap) {
          if (!snap.val()) {
            return
          }
          finRef.off('value', waitForFinish)
          if (options.enableTime) {
            stream.push(raw)
          }
          stream.push(null)
        }
        finRef.on('value', waitForFinish)
      }
    } else if (options.enableTime) {
      stream.push(raw)
    } else {
      stream.push(raw.data, raw.encoding === 'buffer' ? undefined : raw.encoding)
    }
    if (raw.data === null) {
      finish()
    }
  }
  const removeCheck = function (snap) {
    options.node.once('value', function (snap) {
      if (snap.val() === null) {
        lock = true
        if (!finished) {
          if (writable && (!readable || options.allowHalfOpen !== false)) {
            stream.uncork()
            stream.end(null)
          }
          if (readable) {
            stream.resume()
            stream.push(null)
          }
        }
        finish()
      }
    })
  }
  buffer.on('child_removed', removeCheck)
  if (readable) {
    // Duplex stream directly sends back the data
    if (!writable) {
      _waitFor = _waitFor.then(function () {
        return new Promise(function (resolve) {
          const handleInitialValue = function (snap) {
            if (snap.val() === null) {
              return
            }
            buffer.off('value', handleInitialValue)
            if (finished || lock) {
              return // Nothing to do anymore
            }
            buffer.on('child_added', onData)
            snap.forEach(function (child) {
              child.val()
              // onData(child)
            })
            resolve()
          }
          buffer.on('value', handleInitialValue)
        })
      })
      // buffer.on('child_added', onData)
    }
    stream._read = function noop (size) {}
  }
  _waitFor.then(function () {
    _waitFor = Promise.resolve(null)
  })
  if (writable) {
    stream._write = function (chunk, encoding, callback) {
      if (!finished) {
        putNext(chunk, encoding, callback)
      } else {
        setImmediate(callback)
      }
    }
    stream.on('finish', function () {
      if (!finished) {
        putNext(null)
      }
    })
  }
  var _disposed
  stream.dispose = function () {
    if (!_disposed) {
      _disposed = options.node.remove()
    }
    return _disposed
  }
  stream.url = options.node.toString()
  stream.key = options.node.key
  return stream
}
createStream.createReadStream = function (options) {
  return createStream(Object.assign({mode: 'r'}, options))
}
createStream.createWriteStream = function (options) {
  return createStream(Object.assign({mode: 'w'}, options))
}
createStream.createDuplexStream = function (options) {
  return createStream(Object.assign({mode: 'rw'}, options))
}
module.exports = createStream
<commit_msg>fix(state): Unless the encoding is relevant it stops to store the encoding.<commit_after>'use strict'
const Duplex = require('readable-stream').Duplex
const Writable = require('readable-stream').Writable
const Readable = require('readable-stream').Readable

function AppError (code, message) {
  Error.apply(this, [message])
  this.code = code
}
AppError.prototype = Object.create(Error)
AppError.prototype.toString = function () {
  return '[' + this.code + '] ' + this.message
}

const createStream = function (options) {
  if (!options) {
    options = {}
  }
  const rwClean = String(options.mode).trim()
  const readable = rwClean === 'r' || rwClean === 'rw'
  const writable = rwClean === 'w' || rwClean === 'rw'
  if (!options.node) {
    throw new AppError('ERRNODE', 'Option Error: `node` is missing!')
  }
  if (!readable && !writable) {
    throw new AppError('ERRMODE', 'Option Error: `mode` needs to be `r`, `w` or `rw`!')
  }
  const buffer = options.node.child('buffer')
  const finRef = options.node.child('finished')
  var _waitFor
  if (writable) {
    _waitFor = Promise.all([
      options.node.child('started').set(new Date().toISOString()),
      buffer.set([]),
      finRef.set(null)
    ])
  } else {
    _waitFor = Promise.resolve(null)
  }
  if (options.enableTime) {
    // Enable time enforces objectMode on the stream since the times are stored
    // in an object
    options.objectMode = true
  }
  var finished = false
  var lock = false
  var count = 0
  const putNext = function (data, encoding, callback) {
    const payload = {}
    const line = {
      time: new Date().toISOString()
    }
    if (!options.binary && data instanceof Buffer) {
      data = data.toString()
      encoding = 'utf8'
    }
    if (data !== undefined) {
      line.data = data
    }
    if (encoding !== undefined && data instanceof Buffer) {
      line.encoding = encoding
    }
    payload[count] = line
    count += 1
    _waitFor.then(function () {
      return buffer.update(payload)
    }).then(function () {
      if (data === null) {
        return finRef.set(new Date().toISOString()).then(function () {
          return new Promise(function (resolve) {
            setImmediate(resolve)
          })
        })
      }
      return null
    }).then(function () {
      if (readable && !lock) {
        // Duplex stream processing
        if (data === null) {
          if (options.enableTime) {
            stream.push(line)
          }
          stream.push(null)
          finish()
        } else if (options.enableTime) {
          stream.push(line)
        } else if (options.objectMode || data === undefined || typeof data === 'string' || data instanceof Buffer) {
          stream.push(data, options.objectMode ? undefined : encoding)
        } else {
          stream.push(String(data))
        }
      }
      if (callback) {
        callback()
      }
    }).catch(function (err) {
      if (callback) {
        callback(err)
      }
      console.log('WARNING: Error in stream')
      console.log(err.stack || err)
    })
  }
  const finish = function () {
    if (!finished) {
      finished = true
      buffer.off('child_removed', removeCheck)
      buffer.off('child_added', onData)
      return true
    }
    return false
  }
  const stream = readable ? (writable ? new Duplex(options) : new Readable(options)) : new Writable(options)
  const onData = function (snap) {
    var raw = snap.val()
    if (raw === null) {
      // This case should never occur. But if it does through manual manipulation
      // it will not break anything
      return
    }
    if (raw.data === undefined) {
      raw.data = null
    } else if (!(
      raw.data === null ||
      typeof raw.data === 'string' ||
      raw.data instanceof Buffer
    )) {
      if (raw.data.type === 'Buffer') {
        raw.data = Buffer.from(raw.data.data)
      } else if (!options.objectMode) {
        // Make sure that we stringify the data
        raw.data = String(raw.data)
      }
    }
    if (raw.data === null) {
      if (finish()) {
        const waitForFinish = function (snap) {
          if (!snap.val()) {
            return
          }
          finRef.off('value', waitForFinish)
          if (options.enableTime) {
            stream.push(raw)
          }
          stream.push(null)
        }
        finRef.on('value', waitForFinish)
      }
    } else if (options.enableTime) {
      stream.push(raw)
    } else {
      stream.push(raw.data, raw.encoding === 'buffer' ? undefined : raw.encoding)
    }
    if (raw.data === null) {
      finish()
    }
  }
  const removeCheck = function (snap) {
    options.node.once('value', function (snap) {
      if (snap.val() === null) {
        lock = true
        if (!finished) {
          if (writable && (!readable || options.allowHalfOpen !== false)) {
            stream.uncork()
            stream.end(null)
          }
          if (readable) {
            stream.resume()
            stream.push(null)
          }
        }
        finish()
      }
    })
  }
  buffer.on('child_removed', removeCheck)
  if (readable) {
    // Duplex stream directly sends back the data
    if (!writable) {
      _waitFor = _waitFor.then(function () {
        return new Promise(function (resolve) {
          const handleInitialValue = function (snap) {
            if (snap.val() === null) {
              return
            }
            buffer.off('value', handleInitialValue)
            if (finished || lock) {
              return // Nothing to do anymore
            }
            buffer.on('child_added', onData)
            snap.forEach(function (child) {
              child.val()
              // onData(child)
            })
            resolve()
          }
          buffer.on('value', handleInitialValue)
        })
      })
      // buffer.on('child_added', onData)
    }
    stream._read = function noop (size) {}
  }
  _waitFor.then(function () {
    _waitFor = Promise.resolve(null)
  })
  if (writable) {
    stream._write = function (chunk, encoding, callback) {
      if (!finished) {
        putNext(chunk, encoding, callback)
      } else {
        setImmediate(callback)
      }
    }
    stream.on('finish', function () {
      if (!finished) {
        putNext(null)
      }
    })
  }
  var _disposed
  stream.dispose = function () {
    if (!_disposed) {
      _disposed = options.node.remove()
    }
    return _disposed
  }
  stream.url = options.node.toString()
  stream.key = options.node.key
  return stream
}
createStream.createReadStream = function (options) {
  return createStream(Object.assign({mode: 'r'}, options))
}
createStream.createWriteStream = function (options) {
  return createStream(Object.assign({mode: 'w'}, options))
}
createStream.createDuplexStream = function (options) {
  return createStream(Object.assign({mode: 'rw'}, options))
}
module.exports = createStream
<|endoftext|>


<commit_before>var express = require('express');
var app = express();
var npm = new require('npm-api')();
var _ = require('lodash');

var fs = require('fs');
var read = function(name) {return fs.readFileSync(name, { encoding: 'utf-8' })};
var help = read('index.html');

var MongoClient = require('mongodb').MongoClient;
var mongoUrl = process.env.MONGODB_URI;

app.use(express.static('.'));

var customRouter = express.Router();
customRouter.get('*', function redirect(req, res, next) {
    if (req.hostname === 'npm-api.herokuapp.com') {
        console.log('redirect');
        res.redirect(301, 'http://npmapi.invntrm.ru' + req.originalUrl);
        res.end();
    } else {
        next();
    }
});
app.use('*', customRouter)

app.get('/', function (req, res) {
    res.send(help)
});

app.get('/p/:package', function (req, res) {
    var package = req.params.package;
    getPackageUrl(package)
    .then(function(url) { res.end(url); })
    .then(function() { writeLogDb(package); })
    .catch(function(err) { res.status(500).end('error: package not found or server broken: ' + err); });
});

app.get('/go/:package', function (req, res) {
    var package = req.params.package;
    getPackageUrl(package)
    .then(function(url) { res.redirect(url); })
    .then(function() { writeLogDb(package); })
    .catch(function(err) { res.status(500).end('error: package not found or server broken: ' + err); });
});

app.get('/logs', function(req, res) {
    readLogsDb()
    .then(function(docs) {res.send(docs)})
    .catch(function(err) { res.status(500).end('error: server broken: ' + err); });
});

app.listen(process.env.PORT, function () {
    console.log('Example app listening on port ' + process.env.PORT);
});

function getPackageUrl(name, cb) {
    var repo = npm.repo(name);

    return repo.package()
    .then(function(pkg) {
        var hostpath = pkg.repository.url
        .replace(/^.+\/\//, '')
        .replace(/^.+@/g, '')
        .replace(/:/, '/')
        .replace(/\.git$/, '');

        return 'https://' + hostpath;
    });
}

function writeLogDb(name) {
    console.log('> log', name);
    MongoClient.connect(mongoUrl, function(err, db) {
        if (err) console.log('mongo err', err);
        if (!db) return;
        var collection = db.collection(process.env.MONGODB_COLLECTION);
        collection.insertOne({"package": name}, function(err, result) {
            db.close();
        });
    });
}

function readLogsDb() {
    return new Promise(function(res, rej) {
        MongoClient.connect(mongoUrl, function(err, db) {
            if (err) rej(err);
            if (!db) return;
            var collection = db.collection(process.env.MONGODB_COLLECTION);
            collection.find({}).toArray(function(err, docs) {
                res(_(docs).map('package'));
                db.close();
            });
        });
    });
}
<commit_msg>fix require help<commit_after>var express = require('express');
var app = express();
var npm = new require('npm-api')();
var _ = require('lodash');

var fs = require('fs');
var read = function(name) {return fs.readFileSync(name, { encoding: 'utf-8' })};
var help = read(__dirname + '/index.html');

var MongoClient = require('mongodb').MongoClient;
var mongoUrl = process.env.MONGODB_URI;

app.use(express.static('.'));

var customRouter = express.Router();
customRouter.get('*', function redirect(req, res, next) {
    if (req.hostname === 'npm-api.herokuapp.com') {
        console.log('redirect');
        res.redirect(301, 'http://npmapi.invntrm.ru' + req.originalUrl);
        res.end();
    } else {
        next();
    }
});
app.use('*', customRouter)

app.get('/', function (req, res) {
    res.send(help)
});

app.get('/p/:package', function (req, res) {
    var package = req.params.package;
    getPackageUrl(package)
    .then(function(url) { res.end(url); })
    .then(function() { writeLogDb(package); })
    .catch(function(err) { res.status(500).end('error: package not found or server broken: ' + err); });
});

app.get('/go/:package', function (req, res) {
    var package = req.params.package;
    getPackageUrl(package)
    .then(function(url) { res.redirect(url); })
    .then(function() { writeLogDb(package); })
    .catch(function(err) { res.status(500).end('error: package not found or server broken: ' + err); });
});

app.get('/logs', function(req, res) {
    readLogsDb()
    .then(function(docs) {res.send(docs)})
    .catch(function(err) { res.status(500).end('error: server broken: ' + err); });
});

app.listen(process.env.PORT, function () {
    console.log('Example app listening on port ' + process.env.PORT);
});

function getPackageUrl(name, cb) {
    var repo = npm.repo(name);

    return repo.package()
    .then(function(pkg) {
        var hostpath = pkg.repository.url
        .replace(/^.+\/\//, '')
        .replace(/^.+@/g, '')
        .replace(/:/, '/')
        .replace(/\.git$/, '');

        return 'https://' + hostpath;
    });
}

function writeLogDb(name) {
    console.log('> log', name);
    MongoClient.connect(mongoUrl, function(err, db) {
        if (err) console.log('mongo err', err);
        if (!db) return;
        var collection = db.collection(process.env.MONGODB_COLLECTION);
        collection.insertOne({"package": name}, function(err, result) {
            db.close();
        });
    });
}

function readLogsDb() {
    return new Promise(function(res, rej) {
        MongoClient.connect(mongoUrl, function(err, db) {
            if (err) rej(err);
            if (!db) return;
            var collection = db.collection(process.env.MONGODB_COLLECTION);
            collection.find({}).toArray(function(err, docs) {
                res(_(docs).map('package'));
                db.close();
            });
        });
    });
}
<|endoftext|>


<commit_before>var fs = require('fs');
var path = require('path');
var moment = require('moment');
var express = require('express');
var app = express();
var port = process.env.PORT || 8000;
var saveDir = path.resolve(__dirname, 'public', 'data');
var filename = '';
var data = '';

app.configure(function () {
  app.use(express.urlencoded());
  app.use(express.json());
  app.use('/', express.static(__dirname + '/public'));
  app.use('/data', express.static(saveDir));
  app.use('/data', express.directory(saveDir));
});

app.get('/', function (req, res) {
  res.send('');
});

app.post('/', function (req, res) {

  filename = req.query.filename || moment().format('YYYY_MM_DD-HH_mm_ss') + '.json';
  data = JSON.stringify(req.body, null, 2);

  fs.writeFile(path.resolve(saveDir, filename), data, function (err) {
    if (err) throw err;
    res.send(JSON.stringify({res: filename + ' saved'}));
  });

});

app.listen(port);

console.log('listening on port ' + port);<commit_msg>index will now create public/data if it does not already exist<commit_after>var fs = require('fs');
var path = require('path');
var moment = require('moment');
var express = require('express');
var app = express();
var port = process.env.PORT || 8000;
var saveDir = path.resolve(__dirname, 'public', 'data');
var mkdirp = require('mkdirp');
var filename = '';
var data = '';

app.configure(function () {
  app.use(express.urlencoded());
  app.use(express.json());
  app.use('/', express.static(__dirname + '/public'));
  app.use('/data', express.static(saveDir));
  app.use('/data', express.directory(saveDir));
});

app.get('/', function (req, res) {
  res.send('');
});

app.post('/', function (req, res) {

  mkdirp(saveDir, function(err) {

    filename = req.query.filename || moment().format('YYYY_MM_DD-HH_mm_ss') + '.json';
    data = JSON.stringify(req.body, null, 2);

    fs.writeFile(path.resolve(saveDir, filename), data, function (err) {
      if (err) throw err;
      res.send(JSON.stringify({res: filename + ' saved'}));
    });

  });

});

app.listen(port);

console.log('listening on port ' + port);<|endoftext|>


<commit_before>/*jshint node: true*/
var broccoli = require('broccoli');
var Promise  = require('rsvp').Promise;
var walkSync = require('walk-sync');
var builders = [];

function tree(inputTree, filter) {
  var builder = new broccoli.Builder(inputTree);

  builders.push(builder);

  return builder.build().then(function(inputTree) {
    var paths = walkSync(inputTree.directory);

    if (filter) {
      return filter(paths, inputTree);
    }
    return paths;
  });
}

/**
 * Takes a test subject and returns the result of a
 * build with test subject applied to it.
 *
 * @param  {Object} options
 * @property {Function} options.subject The function that is under test
 * @property {String} options.fixturePath The path to the fixtures being used for the test
 * @property {Function} [options.filter] Filtering function that is applied to result of the build.
 * @return {Promise}
 */
function makeTestHelper(options) {
  var cwd = process.cwd();
  return function() {
    var args = arguments;
    return new Promise(function(resolve) {
      process.chdir(options.fixturePath);
      resolve(tree(options.subject.apply(undefined, args), options.filter));
    }).finally(function() {
      process.chdir(cwd);
    });
  };
}

/**
 * Cleans up all the builders and optionally run a
 * callback.
 * @param  {Function} [cb]
 * @return {Promise}
 */
function cleanupBuilders(cb) {
  return Promise.all(builders.map(function(builder) {
    if (cb) {
      cb();
    }
    return builder.cleanup();
  }));
}

module.exports = {
  makeTestHelper: makeTestHelper,
  cleanupBuilders: cleanupBuilders
};<commit_msg>Return tree base path and files found.<commit_after>/*jshint node: true*/
var broccoli = require('broccoli');
var Promise  = require('rsvp').Promise;
var walkSync = require('walk-sync');
var builders = [];

function tree(inputTree, filter) {
  var builder = new broccoli.Builder(inputTree);

  builders.push(builder);

  return builder.build().then(function(inputTree) {
    var paths = walkSync(inputTree.directory);

    if (filter) {
      paths = filter(paths, inputTree);
    }

    return {
      files: paths,
      directory: inputTree.directory
    }
  });
}

/**
 * Takes a test subject and returns the result of a
 * build with test subject applied to it.
 *
 * @param  {Object} options
 * @property {Function} options.subject The function that is under test
 * @property {String} options.fixturePath The path to the fixtures being used for the test
 * @property {Function} [options.filter] Filtering function that is applied to result of the build.
 * @return {Promise}
 */
function makeTestHelper(options) {
  var cwd = process.cwd();
  return function() {
    var args = arguments;
    return new Promise(function(resolve) {
      process.chdir(options.fixturePath);
      resolve(tree(options.subject.apply(undefined, args), options.filter));
    }).finally(function() {
      process.chdir(cwd);
    });
  };
}

/**
 * Cleans up all the builders and optionally run a
 * callback.
 * @param  {Function} [cb]
 * @return {Promise}
 */
function cleanupBuilders(cb) {
  return Promise.all(builders.map(function(builder) {
    if (cb) {
      cb();
    }
    return builder.cleanup();
  }));
}

module.exports = {
  makeTestHelper: makeTestHelper,
  cleanupBuilders: cleanupBuilders
};
<|endoftext|>


<commit_before>var express = require('express');
var app = express();

app.use('/threejs/', express.static('threejs'));
app.use('/new/', express.static('freshjs'));

app.listen(80, function () {
  console.log('Example app listening on port 80!')
});<commit_msg>updating indexjs<commit_after>var express = require('express');
var app = express();

app.use('/', express.static('threejs'));
//app.use('/new/', express.static('freshjs'));

app.listen(80, function () {
  console.log('Example app listening on port 80!')
});<|endoftext|>


<commit_before>var app = require('app')
var meow = require('meow')
var fs = require('fs')
var path = require('path')
var BrowserWindow = require('browser-window')

var wargs = require('./lib/args')
var markdownToHTMLPath = require('./lib/markdown')

app.on('ready', appReady)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

var cli = meow({
  pkg: './package.json',
  help: [
    'Options',
    '  --help                     Show this help',
    '  --version                  Current version of package',
    '  -i | --input               String - The path to the HTML file or url',
    '  -o | --output              String - The path of the output PDF',
    '  -c | --css                 String - The path to custom CSS',
    '  -b | --printBackground     Boolean - Whether to print CSS backgrounds.',
    '                               false - default',
    '  -s | --printSelectionOnly  Boolean - Whether to print selection only',
    '                               false - default',
    '  -l | --landscape           Boolean - true for landscape, false for portrait.',
    '                               false - default',
    '  -m | --marginType          Integer - Specify the type of margins to use',
    '                               0 - default',
    '                               1 - none',
    '                               2 - minimum',
    '',
    'Usage',
    '  $ electron-pdf <input> <output>',
    '  $ electron-pdf <input> <output> -l',
    '',
    'Examples',
    '  $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.pdf',
    '  $ electron-pdf ./index.html ~/Desktop/index.pdf',
    '  $ electron-pdf ./README.md ~/Desktop/README.pdf -l',
    '  $ electron-pdf ./README.md ~/Desktop/README.pdf -l -c my-awesome-css.css',

    ''
  ].join('\n')
})

function appReady () {
  var input = cli.input[0] || cli.flags.i || cli.flags.input
  var output = cli.input[1] || cli.flags.o || cli.flags.output
  var customCss = cli.flags.c || cli.flags.css
  if (!input || !output) {
    cli.showHelp()
    app.quit()
  }

  function isMarkdown (input) {
    var ext = path.extname(input)
    return ext.indexOf('md') > 0 || ext.indexOf('markdown') > 0
  }

  if (isMarkdown(input)) {
    var opts = {}

    if (customCss) {
      opts.customCss = customCss
    }

    // if given a markdown, render it into HTML and return the path of the HTML
    input = markdownToHTMLPath(input, opts, function (err, tmpHTMLPath) {
      if (err) {
        console.error('Parse markdown file error', err)
        app.quit()
      }

      var indexUrl = wargs.urlWithArgs(tmpHTMLPath, {})
      render(indexUrl, output)
    })
  } else {
    var indexUrl = wargs.urlWithArgs(input, {})
    render(indexUrl, output)
  }
}

/**
 * render file to pdf
 * @param  {String} indexUrl The path to the HTML or url
 */
function render (indexUrl, output) {
  var win = new BrowserWindow({ width: 0, height: 0, show: false })
  win.on('closed', function () { win = null })
  win.loadURL(indexUrl)

  // print to pdf args
  var opts = {
    marginType: cli.flags.m || cli.flags.marginType || 0,
    printBackground: cli.flags.p || cli.flags.printBackground || true,
    printSelectionOnly: cli.flags.s || cli.flags.printSelectionOnly || false,
    landscape: cli.flags.l || cli.flags.landscape || false
  }

  win.webContents.on('did-finish-load', function () {
    win.webContents.printToPDF(opts, function (err, data) {
      if (err) {
        console.error(err)
      }

      fs.writeFile(path.join(process.cwd(), output), data, function (err) {
        if (err) {
          console.error(err)
        }
        app.quit()
      })
    })
  })
}
<commit_msg>Use path.resolve on output path.<commit_after>var app = require('app')
var meow = require('meow')
var fs = require('fs')
var path = require('path')
var BrowserWindow = require('browser-window')

var wargs = require('./lib/args')
var markdownToHTMLPath = require('./lib/markdown')

app.on('ready', appReady)

app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

var cli = meow({
  pkg: './package.json',
  help: [
    'Options',
    '  --help                     Show this help',
    '  --version                  Current version of package',
    '  -i | --input               String - The path to the HTML file or url',
    '  -o | --output              String - The path of the output PDF',
    '  -c | --css                 String - The path to custom CSS',
    '  -b | --printBackground     Boolean - Whether to print CSS backgrounds.',
    '                               false - default',
    '  -s | --printSelectionOnly  Boolean - Whether to print selection only',
    '                               false - default',
    '  -l | --landscape           Boolean - true for landscape, false for portrait.',
    '                               false - default',
    '  -m | --marginType          Integer - Specify the type of margins to use',
    '                               0 - default',
    '                               1 - none',
    '                               2 - minimum',
    '',
    'Usage',
    '  $ electron-pdf <input> <output>',
    '  $ electron-pdf <input> <output> -l',
    '',
    'Examples',
    '  $ electron-pdf http://fraserxu.me ~/Desktop/fraserxu.pdf',
    '  $ electron-pdf ./index.html ~/Desktop/index.pdf',
    '  $ electron-pdf ./README.md ~/Desktop/README.pdf -l',
    '  $ electron-pdf ./README.md ~/Desktop/README.pdf -l -c my-awesome-css.css',

    ''
  ].join('\n')
})

function appReady () {
  var input = cli.input[0] || cli.flags.i || cli.flags.input
  var output = cli.input[1] || cli.flags.o || cli.flags.output
  var customCss = cli.flags.c || cli.flags.css
  if (!input || !output) {
    cli.showHelp()
    app.quit()
  }

  function isMarkdown (input) {
    var ext = path.extname(input)
    return ext.indexOf('md') > 0 || ext.indexOf('markdown') > 0
  }

  if (isMarkdown(input)) {
    var opts = {}

    if (customCss) {
      opts.customCss = customCss
    }

    // if given a markdown, render it into HTML and return the path of the HTML
    input = markdownToHTMLPath(input, opts, function (err, tmpHTMLPath) {
      if (err) {
        console.error('Parse markdown file error', err)
        app.quit()
      }

      var indexUrl = wargs.urlWithArgs(tmpHTMLPath, {})
      render(indexUrl, output)
    })
  } else {
    var indexUrl = wargs.urlWithArgs(input, {})
    render(indexUrl, output)
  }
}

/**
 * render file to pdf
 * @param  {String} indexUrl The path to the HTML or url
 */
function render (indexUrl, output) {
  var win = new BrowserWindow({ width: 0, height: 0, show: false })
  win.on('closed', function () { win = null })
  win.loadURL(indexUrl)

  // print to pdf args
  var opts = {
    marginType: cli.flags.m || cli.flags.marginType || 0,
    printBackground: cli.flags.p || cli.flags.printBackground || true,
    printSelectionOnly: cli.flags.s || cli.flags.printSelectionOnly || false,
    landscape: cli.flags.l || cli.flags.landscape || false
  }

  win.webContents.on('did-finish-load', function () {
    win.webContents.printToPDF(opts, function (err, data) {
      if (err) {
        console.error(err)
      }

      fs.writeFile(path.resolve(output), data, function (err) {
        if (err) {
          console.error(err)
        }
        app.quit()
      })
    })
  })
}
<|endoftext|>


<commit_before>/**
 * Runs 10k samples against a Redis server to collect some stats.
 * License: Apache 2.0
 */

const redis = require("redis");

let client = process.argv.length > 2 ?
    redis.createClient(Number.parseInt(process.argv[2].split(':')[1]), process.argv[2].split(':')[0]) :  //with host:port
    redis.createClient(); //default

client.on("error", function (err) {
    console.log("Error " + err);
});

const stats = {
    sampleSize: 0,
    knownKeys: {},
    types: {},
    prefixes: {}
};

function incFreq(hash, item, amount) {
    if (hash[item] === undefined) {
        hash[item] = 1
    } else {
        hash[item] += amount ? amount : 1;
    }
}

function sample(size) {
    stats.sampleSize = size;
    for (let i = 0; i < size; i++) {
        sampleIteration(i);
    }
}

function examineAsync(key) {
    let type = new Promise((resolve, reject) => {
        client.type(key, (err, res) => {
            if (err) reject(err); else resolve(res);
        });
    });
    let ttl = new Promise((resolve, reject) => {
        client.ttl(key, (err, res) => {
            if (err) reject(err); else resolve(res);
        });
    });
    let debug = new Promise((resolve, reject) => {
        client.debug("OBJECT", key, (err, res) => {
            if (err) reject(err); else {
                let arr = res.split(' ');
                let ret = {};
                arr.forEach(kv => {
                    let pair = kv.split(":");
                    if (pair.length > 1) {
                        ret[pair[0]] = isNaN(pair[1]) ? pair[1] : parseFloat(pair[1]);
                    }
                });
                resolve(ret);
            }
        });
    });
    return Promise.all([type, ttl, debug]);
}

function sampleIteration(idx) {
    client.sendCommand('RANDOMKEY', function (err, key) {
        //console.log(idx + ": " + key);
        examineAsync(key).then(v => {
            addSample(key, v[0], v[1], v[2]);
            if (idx === stats.sampleSize - 1) {
                printResults();
                client.quit();
            }
        }).catch(err => console.log(err.message));
    });
}

function addSample(key, type, ttl, debug) {
    if (stats.knownKeys[key] !== undefined) {
        return;
    }
    stats.knownKeys[key] = key;
    incFreq(stats.types, type);
    let splits = key.split(/[\s,:.]/);
    if (splits.length > 1) {
        addPrefixSample(splits[0], type, ttl, debug);
    }
    if (splits.length > 2) {
        addPrefixSample(splits[0]+"."+splits[1], ttl, debug);
    }
}

function addPrefixSample(prefix, ttl, debug) {
    let p = stats.prefixes[prefix];
    if (p === undefined) {
        stats.prefixes[prefix] = p = {};
    }
    incFreq(p,"count");
    incFreq(p,"size",debug.serializedlength);
    if (ttl > 0) {
        incFreq(p, "ttl", ttl);
    } else {
        incFreq(p, "ttl", -1);
    }
}

function formatSize(size) {
    if (size === 0) return "0";
    let i = Math.floor( Math.log(size) / Math.log(1024) );
    return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB'][i];
}

function printResults() {
    stats.knownKeys = {};
    console.log("Redis Server found:"+client.server_info.redis_version);
    console.log("      uptime in sec:"+client.server_info.uptime_in_seconds);
    console.log("      connected clients:"+client.server_info.connected_clients);
    console.log("      used memory:"+client.server_info.used_memory_human);
    console.log("      used memory RSS:"+client.server_info.used_memory_human);
    console.log("      total commands processed:"+client.server_info.total_commands_processed);
    console.log("      total net input:"+formatSize(client.server_info.total_net_input_bytes));
    console.log("      total net output:"+formatSize(client.server_info.total_net_output_bytes));
    console.log("Sample size (via RANDOMKEY):"+stats.sampleSize);
    console.log(" sampled types:"+JSON.stringify(stats.types));
    console.log(" sampled common prefixes:");
    for (let k in stats.prefixes) {
        if (stats.prefixes.hasOwnProperty(k)) {
            let prefix = stats.prefixes[k];
            if (prefix.count < 2) continue; //omit single value prefixes
            console.log(`    [${k}] count:${prefix.count} avg size:${(prefix.size / prefix.count).toFixed(2)} avg ttl:${(prefix.ttl/prefix.count).toFixed(0)}`);
        }
    }
}

sample(10000);

<commit_msg>Made param parsing more readable and added TB sizes to formatter<commit_after>/**
 * Runs 10k samples against a Redis server to collect some stats.
 * License: Apache 2.0
 */

const redis = require("redis");
let host = '127.0.0.1';
let port = 6379;

//parse optional arguments
if (process.argv.length > 2) {
    let redisURL = process.argv[2].split(":");
    host = redisURL[0];
    if (redis[1] !== undefined) {
        port = Number.parseInt(redisURL[1]);
    }
}

//create Redis client connection
let client = redis.createClient(port,host);

client.on("error", function (err) {
    console.log("Error " + err);
});

const stats = {
    sampleSize: 0,
    knownKeys: {},
    types: {},
    prefixes: {}
};

function incFreq(hash, item, amount) {
    if (hash[item] === undefined) {
        hash[item] = 1
    } else {
        hash[item] += amount ? amount : 1;
    }
}

function sample(size) {
    stats.sampleSize = size;
    for (let i = 0; i < size; i++) {
        sampleIteration(i);
    }
}

function examineAsync(key) {
    let type = new Promise((resolve, reject) => {
        client.type(key, (err, res) => {
            if (err) reject(err); else resolve(res);
        });
    });
    let ttl = new Promise((resolve, reject) => {
        client.ttl(key, (err, res) => {
            if (err) reject(err); else resolve(res);
        });
    });
    let debug = new Promise((resolve, reject) => {
        client.debug("OBJECT", key, (err, res) => {
            if (err) reject(err); else {
                let arr = res.split(' ');
                let ret = {};
                arr.forEach(kv => {
                    let pair = kv.split(":");
                    if (pair.length > 1) {
                        ret[pair[0]] = isNaN(pair[1]) ? pair[1] : parseFloat(pair[1]);
                    }
                });
                resolve(ret);
            }
        });
    });
    return Promise.all([type, ttl, debug]);
}

function sampleIteration(idx) {
    client.sendCommand('RANDOMKEY', function (err, key) {
        //console.log(idx + ": " + key);
        examineAsync(key).then(v => {
            addSample(key, v[0], v[1], v[2]);
            if (idx === stats.sampleSize - 1) {
                printResults();
                client.quit();
            }
        }).catch(err => console.log(err.message));
    });
}

function addSample(key, type, ttl, debug) {
    if (stats.knownKeys[key] !== undefined) {
        return;
    }
    stats.knownKeys[key] = key;
    incFreq(stats.types, type);
    let splits = key.split(/[\s,:.]/);
    if (splits.length > 1) {
        addPrefixSample(splits[0], type, ttl, debug);
    }
    if (splits.length > 2) {
        addPrefixSample(splits[0]+"."+splits[1], ttl, debug);
    }
}

function addPrefixSample(prefix, ttl, debug) {
    let p = stats.prefixes[prefix];
    if (p === undefined) {
        stats.prefixes[prefix] = p = {};
    }
    incFreq(p,"count");
    incFreq(p,"size",debug.serializedlength);
    if (ttl > 0) {
        incFreq(p, "ttl", ttl);
    } else {
        incFreq(p, "ttl", -1);
    }
}

function formatSize(size) {
    if (size === 0) return "0";
    let i = Math.floor( Math.log(size) / Math.log(1024) );
    return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}

function printResults() {
    stats.knownKeys = {};
    console.log("Redis Server found:"+client.server_info.redis_version);
    console.log("      uptime in sec:"+client.server_info.uptime_in_seconds);
    console.log("      connected clients:"+client.server_info.connected_clients);
    console.log("      used memory:"+client.server_info.used_memory_human);
    console.log("      used memory RSS:"+client.server_info.used_memory_human);
    console.log("      total commands processed:"+client.server_info.total_commands_processed);
    console.log("      total net input:"+formatSize(client.server_info.total_net_input_bytes));
    console.log("      total net output:"+formatSize(client.server_info.total_net_output_bytes));
    console.log("Sample size (via RANDOMKEY):"+stats.sampleSize);
    console.log(" sampled types:"+JSON.stringify(stats.types));
    console.log(" sampled common prefixes:");
    for (let k in stats.prefixes) {
        if (stats.prefixes.hasOwnProperty(k)) {
            let prefix = stats.prefixes[k];
            if (prefix.count < 2) continue; //omit single value prefixes
            console.log(`    [${k}] count:${prefix.count} avg size:${(prefix.size / prefix.count).toFixed(2)} avg ttl:${(prefix.ttl/prefix.count).toFixed(0)}`);
        }
    }
}

sample(10000);

<|endoftext|>


<commit_before>// Use Hilbert curve point generation to map 2D data to 1D space and vice-versa.
// Will later expand to allow `n` dimensions.

function Point(x, y, z) {
    this.rotations = {
        x: 0,
        y: 0,
        z: 0
    }

    this.init = function (x, y, z) {
        this.x = Math.round(x) || 0
        this.y = Math.round(y) || 0
        if (z != null) {
            this.z = Math.round(z) || 0
            this.d = 3
            this.n = 4 * this.z + 2 * this.y  + this.x
        } else {
            this.z = null
            this.d = 2
        }
    }
    if (x instanceof Array) {
        this.init(x[0], x[1], x[2])
    } else {
        this.init(x, y, z)
    }
}

Point.prototype.rotate = function (p, n) { // :: Point -> Int -> Point
    // record rotations
    if (p.n == 0) return new Point(this.z, this.x, this.y)
    if (p.n == 1 || p.n == 3) return new Point(this.y, this.z, this.x)
    if (p.n == 2 || p.n == 6) return new Point(n - this.x, n - this.y, this.z)
    return new Point(n - this.z, this.x, n - this.y)
}

Point.prototype.rotate2d = function (n, xbit, ybit) { // : Int -> Int -> Int -> Point
    return new Point(rotate2d(n, this.x, this.y, xbit, ybit))
}

Point.prototype.rotate3d = function (level) { // :: Int -> Point
    return new Point(rotate3d(level, this.x, this.y, this.z))
}

Point.prototype.rotateLeft = function (n) { // :: Int -> Point
    if (n % 3 == 0) return this
    if (n % 3 == 1) return new Point(this.y, this.z, this.x)
    return new Point(this.z, this.x, this.y)
}

Point.prototype.rotateRight = function (n) { // :: Int -> Point
    if (n % 3 == 0) return this
    if (n % 3 == 1) return new Point(this.z, this.x, this.y)
    return new Point(this.y, this.z, this.x)
}

Point.prototype.toArray = function () { // :: -> [Int, Int]
        if (this.d == 3) { return [this.x, this.y, this.z] }
        return [this.x, this.y]
}

Point.prototype.mod = function (n) { // :: Int -> Point
    return new Point(this.x % n, this.y % n, this.z % n)
}

Point.prototype.unrotate = function (n) {
    // read this.rotations and undo
}

// Accepts the height or width of a square/graph, and the coordinates to
// convert.
function convert2dPointToDistance (p, height) { // :: Int -> Int -> Int -> Int
    var xbit, ybit, level, d = 0
    var forHeight = p.x > p.y ? p.x : p.y

    // needs some tests to make sure height is compatible
    // What keeps the user from putting 54 down as the height
    while (forHeight >= height) {
            height *=2
    }
    // For each Hilbert level, we want to add an amount to
    // `d` based on which region we are in
    for (level = height / 2; level > 0; level = Math.floor(level / 2)) {
        // Determine what region we're in
        xbit = (p.x & level) > 0
        ybit = (p.y & level) > 0
        // increase distance based on region
        d += level * level * ((3 * xbit) ^ ybit)
        // rotate so that we'll be in sync with the next
        // region.
        p = p.rotate2d(level, xbit, ybit)
        // HEAD
        // p = p.rotate2d(height, xbit, ybit)
    }

    return d
}

// height and coordinates.
function convert3dPointToDistance (p, height) { // :: Int -> Int -> Int -> Int -> Int
    var s = 1, level = 0
    var max = Math.max.apply(Math, p.toArray())
    for (; 2 * s <= max; s *= 2) {
        level = (level + 1) % 3
    }

    // shuffle axes
    // rotate based on parity
}

// Accepts height or width of a square/graph and distance
function convertDistanceTo2dPoint (distance, height) { // :: Int -> Int -> [Int, Int]
    distance = Math.floor(distance)
    var xbit, ybit, level, p = new Point(0, 0)

    if (height <= Math.sqrt(distance))  {
        height = 2
        while (height <= Math.sqrt(distance)) {
            height *=2
        }
    }

    for (level = 1; level < height; level *= 2) {
        xbit = 1 & (distance / 2)
        ybit = 1 & (distance ^ xbit)

        p = p.rotate2d(level, xbit, ybit)
        p.x += level * xbit
        p.y += level * ybit
        distance = Math.floor(distance / 4)
    }

    return p.toArray()
}

// height/width of a square/graph and distance
function convertDistanceTo3dPoint (distance, height) { // Int -> Int -> [Int, Int, Int]
    distance = Math.floor(distance)
    var xbit, ybit, zbit, level, parity
    var iter = 2, log = 0, p = new Point(0, 0, 0)

    height = height || 2

    // vvv this is interesting.
    for (parity = 1; parity < height; parity *= 2, ++log) {}
    parity = log % 3;

    // the `|| distance` was removed from 2d version
    for (level = 1; level < height || distance > 0; level *=2) {
        xbit = distance & 1;
        ybit = (distance / 2) & 1;
        zbit = (distance / 4) & 1;

        var temp = rotate3d(level - 1, xbit ^ ybit, ybit ^ zbit, zbit)
        p.x = temp[0] * level + level - 1
        p.y = temp[1] * level + level - 1
        p.z = temp[2] * level + level - 1

        distance = Math.floor(distance / 8)
        level *= 2;
        iter++;
    }

    return p.rotateLeft(iter - parity + 1).toArray();
}

// Rotate the coordinate plane and (x,y)
function rotate2d (n, x, y, xbit, ybit) { // :: Int -> Int -> Int -> Int -> Int -> [Int, Int]
    if (ybit == 0  ) {
        if (xbit == 1) {
            x = n - 1 - x
            y = n - 1 - y
        }

        var temp = x
        x = y
        y = temp
    }

    return [x, y]
}

// Rotate the coordinate plane and (x,y, z)
function rotate3d(level, x, y, z) { // :: Int -> Int -> Int -> Int -> [Int, Int, Int]
    var index = 4 * z + 2 * y + x
    if (index == 0) {
        return [z, x, y]
    } else if (index == 1 || index == 3) {
        return [y, z, x]
    } else if (index == 2 || index == 6) {
        return [level - x, level - y, z]
    } else if (index == 5 || index == 7) {
        return [y, level - z, level - x]
    } else {
        return [level - z, x, level - y]
    }
}

// returns a non-negative int 'inverse' such that graycode(inverse) = g
function grayInverse (g) { // : Int -> Int
    var m = bits(g), inverse = g, j = 1
    while (j < m) {
        inverse = inverse ^ (g >> j)
        j++
    }
    return inverse
}

// Returns the number of bits required to store an integer
function bits (n) { // :: Int > Int
    var ret = 0
    while (n > 0) {
        ret++
        n = n >> 1
    }
    return ret
}

// Rotates the bits of x to the right by n. no sign preservation.
function bitwiseRotateRight (x, n) { // :: Int -> Int -> Int
    var y = (x >> n) & ~(-1 << (32 - n))
    var z = x << (32 - n)
    return y | z
}

// Rotates the bits of x to the left by n. ''
function bitwiseRotateLeft (x, n) { // :: Int -> Int -> Int
    return (x << n) | (x >> (32 - n)) & ~(-1 << n)
}

function grayTransform (entry, direction, x) { // :: Int -> Int -> Int -> Int
    return bitwiseRotateRight((x ^ entry), direction + 1)
}

function hilbertIndex(dim, precision, point) {
    var index = 0, entry = 0, direction = 0, i = precision - 1, arr = point.toArray(), code
    // dim = n
    // precision = m
    // p = point (5,6)
    // vvv this transformation needs to happen
    // entry = e
    // direction = d <- this is n-1. Interesting its set as 0.
    // code = w
    // index = h

    while (i >= 0) {
        // l = [bit(p sub n-1 ; i), bit(p sub n 0 ; i)] [11], [10], [01]
        var bits = 0

        for (var k = 0; k < arr.length; k++) {
            if (arr[arr.length - (k+1)] & (1 << (i - 1))) {
                bits |= 1 << k
            }
        }

        // vv look into each of these variables as well as the functions
        // does bits go in as a string?
        bits = grayTransform(entry, direction, bits) // transform <- 3, 2, 1
        code = grayInverse(bits) // 2, 3, 1

        // vvv new entry direction and index
        entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1)//0,3,3
        direction = direction + (direction * code) + (1 % dim) //<- 1,0,0
        index = (index << dim) | code // <-2, 11, 45

        i--
    }

    return index // set B subscript M
}

exports.xy2d = function (x, y, height) {
    height = height || 2
    return convert2dPointToDistance(new Point(x, y), height)
}

// This needs to be fixed to not return undefined.
exports.xyz2d = function(x, y, z, height) {
    height = height || 2
    return convert3dPointToDistance(new Point(x, y, z), height)
}
exports.d2xy = convertDistanceTo2dPoint
exports.d2xyz = convertDistanceTo3dPoint
exports.hilbert = function (dim, m, x, y, z) {
    return hilbertIndex(dim, m, new Point(x, y, z))
}
exports.gt = grayTransform
<commit_msg>Remove precision parameter; comments.<commit_after>// Use Hilbert curve point generation to map 2D data to 1D space and vice-versa.
// Will later expand to allow `n` dimensions.

function Point(x, y, z) {
    this.rotations = {
        x: 0,
        y: 0,
        z: 0
    }

    this.init = function (x, y, z) {
        this.x = Math.round(x) || 0
        this.y = Math.round(y) || 0
        if (z != null) {
            this.z = Math.round(z) || 0
            this.d = 3
            this.n = 4 * this.z + 2 * this.y  + this.x
        } else {
            this.z = null
            this.d = 2
        }
    }
    if (x instanceof Array) {
        this.init(x[0], x[1], x[2])
    } else {
        this.init(x, y, z)
    }
}

Point.prototype.rotate = function (p, n) { // :: Point -> Int -> Point
    // record rotations
    if (p.n == 0) return new Point(this.z, this.x, this.y)
    if (p.n == 1 || p.n == 3) return new Point(this.y, this.z, this.x)
    if (p.n == 2 || p.n == 6) return new Point(n - this.x, n - this.y, this.z)
    return new Point(n - this.z, this.x, n - this.y)
}

Point.prototype.rotate2d = function (n, xbit, ybit) { // : Int -> Int -> Int -> Point
    return new Point(rotate2d(n, this.x, this.y, xbit, ybit))
}

Point.prototype.rotate3d = function (level) { // :: Int -> Point
    return new Point(rotate3d(level, this.x, this.y, this.z))
}

Point.prototype.rotateLeft = function (n) { // :: Int -> Point
    if (n % 3 == 0) return this
    if (n % 3 == 1) return new Point(this.y, this.z, this.x)
    return new Point(this.z, this.x, this.y)
}

Point.prototype.rotateRight = function (n) { // :: Int -> Point
    if (n % 3 == 0) return this
    if (n % 3 == 1) return new Point(this.z, this.x, this.y)
    return new Point(this.y, this.z, this.x)
}

Point.prototype.toArray = function () { // :: -> [Int, Int]
        if (this.d == 3) { return [this.x, this.y, this.z] }
        return [this.x, this.y]
}

Point.prototype.mod = function (n) { // :: Int -> Point
    return new Point(this.x % n, this.y % n, this.z % n)
}

Point.prototype.unrotate = function (n) {
    // read this.rotations and undo
}

// Accepts the height or width of a square/graph, and the coordinates to
// convert.
function convert2dPointToDistance (p, height) { // :: Int -> Int -> Int -> Int
    var xbit, ybit, level, d = 0
    var forHeight = p.x > p.y ? p.x : p.y

    // needs some tests to make sure height is compatible
    // What keeps the user from putting 54 down as the height
    while (forHeight >= height) {
            height *=2
    }
    // For each Hilbert level, we want to add an amount to
    // `d` based on which region we are in
    for (level = height / 2; level > 0; level = Math.floor(level / 2)) {
        // Determine what region we're in
        xbit = (p.x & level) > 0
        ybit = (p.y & level) > 0
        // increase distance based on region
        d += level * level * ((3 * xbit) ^ ybit)
        // rotate so that we'll be in sync with the next
        // region.
        p = p.rotate2d(level, xbit, ybit)
        // HEAD
        // p = p.rotate2d(height, xbit, ybit)
    }

    return d
}

// height and coordinates.
function convert3dPointToDistance (p, height) { // :: Int -> Int -> Int -> Int -> Int
    var s = 1, level = 0
    var max = Math.max.apply(Math, p.toArray())
    for (; 2 * s <= max; s *= 2) {
        level = (level + 1) % 3
    }

    // shuffle axes
    // rotate based on parity
}

// Accepts height or width of a square/graph and distance
function convertDistanceTo2dPoint (distance, height) { // :: Int -> Int -> [Int, Int]
    distance = Math.floor(distance)
    var xbit, ybit, level, p = new Point(0, 0)

    if (height <= Math.sqrt(distance))  {
        height = 2
        while (height <= Math.sqrt(distance)) {
            height *=2
        }
    }

    for (level = 1; level < height; level *= 2) {
        xbit = 1 & (distance / 2)
        ybit = 1 & (distance ^ xbit)

        p = p.rotate2d(level, xbit, ybit)
        p.x += level * xbit
        p.y += level * ybit
        distance = Math.floor(distance / 4)
    }

    return p.toArray()
}

// height/width of a square/graph and distance
function convertDistanceTo3dPoint (distance, height) { // Int -> Int -> [Int, Int, Int]
    distance = Math.floor(distance)
    var xbit, ybit, zbit, level, parity
    var iter = 2, log = 0, p = new Point(0, 0, 0)

    height = height || 2

    // vvv this is interesting.
    for (parity = 1; parity < height; parity *= 2, ++log) {}
    parity = log % 3;

    // the `|| distance` was removed from 2d version
    for (level = 1; level < height || distance > 0; level *=2) {
        xbit = distance & 1;
        ybit = (distance / 2) & 1;
        zbit = (distance / 4) & 1;

        var temp = rotate3d(level - 1, xbit ^ ybit, ybit ^ zbit, zbit)
        p.x = temp[0] * level + level - 1
        p.y = temp[1] * level + level - 1
        p.z = temp[2] * level + level - 1

        distance = Math.floor(distance / 8)
        level *= 2;
        iter++;
    }

    return p.rotateLeft(iter - parity + 1).toArray();
}

// Rotate the coordinate plane and (x,y)
function rotate2d (n, x, y, xbit, ybit) { // :: Int -> Int -> Int -> Int -> Int -> [Int, Int]
    if (ybit == 0  ) {
        if (xbit == 1) {
            x = n - 1 - x
            y = n - 1 - y
        }

        var temp = x
        x = y
        y = temp
    }

    return [x, y]
}

// Rotate the coordinate plane and (x,y, z)
function rotate3d(level, x, y, z) { // :: Int -> Int -> Int -> Int -> [Int, Int, Int]
    var index = 4 * z + 2 * y + x
    if (index == 0) {
        return [z, x, y]
    } else if (index == 1 || index == 3) {
        return [y, z, x]
    } else if (index == 2 || index == 6) {
        return [level - x, level - y, z]
    } else if (index == 5 || index == 7) {
        return [y, level - z, level - x]
    } else {
        return [level - z, x, level - y]
    }
}

// returns a non-negative int 'inverse' such that graycode(inverse) = g
function grayInverse (g) { // : Int -> Int
    var m = precision(g), inverse = g, j = 1
    while (j < m) {
        inverse = inverse ^ (g >> j)
        j++
    }
    return inverse
}

// Returns the number of bits required to store an integer
function precision (n) { // :: Int > Int
    var ret = 0
    while (n > 0) {
        ret++
        n = n >> 1
    }
    return ret
}

// Rotates the bits of x to the right by n. no sign preservation.
function bitwiseRotateRight (x, n) { // :: Int -> Int -> Int
    var y = (x >> n) & ~(-1 << (32 - n))
    var z = x << (32 - n)
    return y | z
    /*
    return (x >> n) | (x << (32 - n)) & ~(-1 >> n)
    */
}

// Rotates the bits of x to the left by n. ''
function bitwiseRotateLeft (x, n) { // :: Int -> Int -> Int
    return (x << n) | (x >> (32 - n)) & ~(-1 << n)
    //return (x << n) | (x >> (precision(x) - n)) & ~(-1 << n)
}

function grayTransform (entry, direction, x) { // :: Int -> Int -> Int -> Int
    return bitwiseRotateRight((x ^ entry), direction + 1)
}

function hilbertIndex(dim, point) {
    var index = 0, entry = 0, direction = 0, arr = point.toArray(), code,
        i = precision(Math.max.apply(null, arr)) - 1
    // dim = n
    // precision = m
    // p = point (5,6)
    // vvv this transformation needs to happen
    // entry = e
    // direction = d <- this is n-1. Interesting its set as 0.
    // code = w
    // index = h

    while (i >= 0) {
        // l = [bit(p sub n-1 ; i), bit(p sub n 0 ; i)] [11], [10], [01]
        var bits = 0

        for (var k = 0; k < arr.length; k++) {
            if (arr[arr.length - (k+1)] & (1 << (i - 1))) {
                bits |= 1 << k
            }
        }

        // vv look into each of these variables as well as the functions
        // does bits go in as a string?
        bits = grayTransform(entry, direction, bits) // transform <- 3, 2, 1
        code = grayInverse(bits) // 2, 3, 1

        // vvv new entry direction and index
        entry = entry ^ bitwiseRotateLeft((entry * code), direction + 1)//0,3,3
        direction = direction + (direction * code) + (1 % dim) //<- 1,0,0
        index = (index << dim) | code // <-2, 11, 45

        i--
    }

    return index // set B subscript M
}

exports.xy2d = function (x, y, height) {
    height = height || 2
    return convert2dPointToDistance(new Point(x, y), height)
}

// This needs to be fixed to not return undefined.
exports.xyz2d = function(x, y, z, height) {
    height = height || 2
    return convert3dPointToDistance(new Point(x, y, z), height)
}
exports.d2xy = convertDistanceTo2dPoint
exports.d2xyz = convertDistanceTo3dPoint
exports.hilbert = function (dim, x, y, z) {
    return hilbertIndex(dim, new Point(x, y, z))
}
exports.rotl = bitwiseRotateLeft
exports.rotr = bitwiseRotateRight
<|endoftext|>


<commit_before>/*
	The MIT License (MIT)

	Copyright (c) 2014 RangerMauve

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/

var escapeRegex = require('escape-string-regexp');

module.exports = parse;

/**
 * Parses topic string with parameters
 * @param topic Topic string with optional params
 * @returns {Object} Compiles a regex for matching topics, getParams for getting params, and exec for doing both
 */
function parse(topic) {
	var tokens = tokenize(topic).map(process_token);
	var result = {
		regex: make_regex(tokens),
		getParams: make_pram_getter(tokens),
		topic: make_clean_topic(tokens)
	};
	result.exec = exec.bind(result);
	return result;
};

/**
 * Matches regex against topic, returns params if successful
 * @param topic Topic to match
 */
function exec(topic) {
	var regex = this.regex;
	var getParams = this.getParams;
	var match = regex.exec(topic);
	if (match) return getParams(match);
}

// Split the topic into consumable tokens
function tokenize(topic) {
	return topic.split("/");
}

// Processes token and determines if it's a `single`, `multi` or `raw` token
// Each token contains the type, an optional parameter name, and a piece of the regex
// The piece can have a different syntax for when it is last
function process_token(token, index, tokens) {
	var last = (index === (tokens.length - 1));
	if (token[0] === "+") return process_single(token, last);
	else if (token[0] === "#") return process_multi(token, last);
	else return process_raw(token, last);
}

// Processes a token for single paths (prefixed with a +)
function process_single(token) {
	var name = token.slice(1);
	return {
		type: "single",
		name: name,
		piece: "([\\d\\w]+/)",
		last: "([\\d\\w]+/?)"
	};
}

// Processes a token for multiple paths (prefixed with a #)
function process_multi(token, last) {
	if (!last) throw new Error("# wildcard must be at the end of the pattern");
	var name = token.slice(1);
	return {
		type: "multi",
		name: name,
		piece: "((?:[\\d\\w]+/)*)",
		last: "((?:[\\d\\w]+/?)*)"
	}
}

// Processes a raw string for the path, no special logic is expected
function process_raw(token) {
	var token = escapeRegex(token);
	return {
		type: "raw",
		piece: token + "/",
		last: token + "/?"
	};
}

// Turn a topic pattern into a regular MQTT topic
function make_clean_topic(tokens) {
	return tokens.map(function (token) {
		if (token.type === "raw") return token.piece.slice(1);
		else if (token.type === "single") return "+";
		else if (token.type === "multi") return "#";
		else return ""; // Wat
	}).join("/");
}

// Generates the RegExp object from the tokens
function make_regex(tokens) {
	var str = tokens.reduce(function (res, token, index) {
			var is_last = (index == (tokens.length - 1));
			var before_multi = (index === (tokens.length - 2)) && (last(tokens).type == "multi");
			return res + ((is_last || before_multi) ? token.last : token.piece);
		},
		"");
	return new RegExp("^" + str + "$");
}

// Generates the function for getting the params object from the regex results
function make_pram_getter(tokens) {
	return function (results) {
		// Get only the capturing tokens
		var capture_tokens = remove_raw(tokens);
		var res = {};

		// If the regex didn't actually match, just return an empty object
		if (!results) return res;

		// Remove the first item and iterate through the capture groups
		results.slice(1).forEach(function (capture, index) {
			// Retreive the token description for the capture group
			var token = capture_tokens[index];
			var param = capture;
			// If the token doesn't have a name, continue to next group
			if (!token.name) return;

			// If the token is `multi`, split the capture along `/`, remove empty items
			if (token.type === "multi") {
				param = capture.split("/");
				if (!last(param))
					param = remove_last(param);
				// Otherwise, remove any trailing `/`
			} else if (last(capture) === "/")
				param = remove_last(capture);
			// Set the param on the result object
			res[token.name] = param;
		});
		return res;
	}
}

// Removes any tokens of type `raw`
function remove_raw(tokens) {
	return tokens.filter(function (token) {
		return (token.type !== "raw");
	})
}

// Gets the last item or character
function last(items) {
	return items[items.length - 1];
}

// Returns everything but the last item or character
function remove_last(items) {
	return items.slice(0, items.length - 1);
}
<commit_msg>Properly fix clean topic<commit_after>/*
	The MIT License (MIT)

	Copyright (c) 2014 RangerMauve

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/

var escapeRegex = require('escape-string-regexp');

module.exports = parse;

/**
 * Parses topic string with parameters
 * @param topic Topic string with optional params
 * @returns {Object} Compiles a regex for matching topics, getParams for getting params, and exec for doing both
 */
function parse(topic) {
	var tokens = tokenize(topic).map(process_token);
	var result = {
		regex: make_regex(tokens),
		getParams: make_pram_getter(tokens),
		topic: make_clean_topic(tokens)
	};
	result.exec = exec.bind(result);
	return result;
};

/**
 * Matches regex against topic, returns params if successful
 * @param topic Topic to match
 */
function exec(topic) {
	var regex = this.regex;
	var getParams = this.getParams;
	var match = regex.exec(topic);
	if (match) return getParams(match);
}

// Split the topic into consumable tokens
function tokenize(topic) {
	return topic.split("/");
}

// Processes token and determines if it's a `single`, `multi` or `raw` token
// Each token contains the type, an optional parameter name, and a piece of the regex
// The piece can have a different syntax for when it is last
function process_token(token, index, tokens) {
	var last = (index === (tokens.length - 1));
	if (token[0] === "+") return process_single(token, last);
	else if (token[0] === "#") return process_multi(token, last);
	else return process_raw(token, last);
}

// Processes a token for single paths (prefixed with a +)
function process_single(token) {
	var name = token.slice(1);
	return {
		type: "single",
		name: name,
		piece: "([\\d\\w]+/)",
		last: "([\\d\\w]+/?)"
	};
}

// Processes a token for multiple paths (prefixed with a #)
function process_multi(token, last) {
	if (!last) throw new Error("# wildcard must be at the end of the pattern");
	var name = token.slice(1);
	return {
		type: "multi",
		name: name,
		piece: "((?:[\\d\\w]+/)*)",
		last: "((?:[\\d\\w]+/?)*)"
	}
}

// Processes a raw string for the path, no special logic is expected
function process_raw(token) {
	var token = escapeRegex(token);
	return {
		type: "raw",
		piece: token + "/",
		last: token + "/?"
	};
}

// Turn a topic pattern into a regular MQTT topic
function make_clean_topic(tokens) {
	return tokens.map(function (token) {
		if (token.type === "raw") return token.piece.slice(0, -1);
		else if (token.type === "single") return "+";
		else if (token.type === "multi") return "#";
		else return ""; // Wat
	}).join("/");
}

// Generates the RegExp object from the tokens
function make_regex(tokens) {
	var str = tokens.reduce(function (res, token, index) {
			var is_last = (index == (tokens.length - 1));
			var before_multi = (index === (tokens.length - 2)) && (last(tokens).type == "multi");
			return res + ((is_last || before_multi) ? token.last : token.piece);
		},
		"");
	return new RegExp("^" + str + "$");
}

// Generates the function for getting the params object from the regex results
function make_pram_getter(tokens) {
	return function (results) {
		// Get only the capturing tokens
		var capture_tokens = remove_raw(tokens);
		var res = {};

		// If the regex didn't actually match, just return an empty object
		if (!results) return res;

		// Remove the first item and iterate through the capture groups
		results.slice(1).forEach(function (capture, index) {
			// Retreive the token description for the capture group
			var token = capture_tokens[index];
			var param = capture;
			// If the token doesn't have a name, continue to next group
			if (!token.name) return;

			// If the token is `multi`, split the capture along `/`, remove empty items
			if (token.type === "multi") {
				param = capture.split("/");
				if (!last(param))
					param = remove_last(param);
				// Otherwise, remove any trailing `/`
			} else if (last(capture) === "/")
				param = remove_last(capture);
			// Set the param on the result object
			res[token.name] = param;
		});
		return res;
	}
}

// Removes any tokens of type `raw`
function remove_raw(tokens) {
	return tokens.filter(function (token) {
		return (token.type !== "raw");
	})
}

// Gets the last item or character
function last(items) {
	return items[items.length - 1];
}

// Returns everything but the last item or character
function remove_last(items) {
	return items.slice(0, items.length - 1);
}
<|endoftext|>


<commit_before>/* eslint-env node */
/* eslint quotes: [2, "single", "avoid-escape"] */
module.exports = {
  'extends': 'eslint:recommended',
  'rules': {
    'array-bracket-spacing': [2, 'never'],
    'block-scoped-var': 2,
    'brace-style': [2, 'stroustrup', { 'allowSingleLine': false }],
    'camelcase': 1,
    'curly': [2, 'multi-line'],
    'eol-last': 2,
    'eqeqeq': [2, 'smart'],
    'max-depth': [1, 3],
    'max-statements': [1, 25],
    'max-len': [1, 120],
    'new-cap': [
      2,
      {
        newIsCap: true,
        capIsNew: true,
        newIsCapExceptions: [],
        capIsNewExceptions: ['Router', 'Resource', 'Schema', 'ObjectId', 'Q']
      }
    ],
    'no-extend-native': 2,
    'no-mixed-spaces-and-tabs': 2,
    'no-trailing-spaces': 2,
    'no-use-before-define': [2, 'nofunc'],
    'no-unused-vars': 1,
    'object-curly-spacing': [2, 'never'],
    'quotes': [2, 'single', 'avoid-escape'],
    'semi': [2, 'always'],
    'space-after-keywords': [2, 'always'],
    'space-unary-ops': 2,
    'no-underscore-dangle': 0
  }
};
<commit_msg>Adding new eslint rules.<commit_after>/* eslint-env node */
/* eslint quotes: [2, "single", "avoid-escape"] */
module.exports = {
  'extends': 'eslint:recommended',
  'rules': {
    'array-bracket-spacing': [2, 'never'],
    'block-scoped-var': 2,
    'brace-style': [2, 'stroustrup', { 'allowSingleLine': false }],
    'camelcase': 2,
    'callback-return': [2, ["callback", "_callback", "_cb", "cb", "next", "_next"]],
    'comma-style': 2,
    'curly': [2, 'multi-line'],
    'eol-last': 2,
    'eqeqeq': [2, 'smart'],
    'max-depth': [1, 3],
    'max-statements': [1, 25],
    'max-len': [1, 120],
    'new-cap': [
      2,
      {
        newIsCap: true,
        capIsNew: true,
        newIsCapExceptions: [],
        capIsNewExceptions: ['Router', 'Resource', 'Schema', 'ObjectId', 'Q']
      }
    ],
    'no-cond-assign': 2,
    'no-extend-native': 2,
    'no-extra-semi': 2,
    'no-mixed-spaces-and-tabs': 2,
    'no-multiple-empty-lines': 2,
    'no-spaced-func': 2,
    'no-trailing-spaces': 2,
    'no-underscore-dangle': 0,
    'no-use-before-define': [2, 'nofunc'],
    'no-unexpected-multiline': 2,
    'no-unused-vars': 1,
    'object-curly-spacing': [2, 'never'],
    'padded-blocks': [2, 'never'],
    'quotes': [2, 'single', 'avoid-escape'],
    'semi': [2, 'always'],
    'space-after-keywords': [2, 'always'],
    'space-before-blocks': 2,
    'space-before-function-paren': [2, 'never'],
    'space-unary-ops': 2
  }
};
<|endoftext|>


<commit_before>var _ = require('lodash'),
  moment = require('moment'),
  parser = require('subtitles-parser'),
  fs = require('fs');

var text2srt = (function() {
  var mod = {};

  mod.convert = function(data) {
    var srtObject = function() {
      this.id = '0';
      this.startTime = '00:00:00,000';
      this.endTime = '00:00:00,000';
      this.text = '';
    },
      arrayOfSrt = [];

    var srt = data.split("//");
    _.each(srt, function(el, index) {
      var instance = new srtObject();
      instance.id = index + 1;
      instance.text = el;
      var startTime = index + 1 + 0.200;
      instance.startTime = moment(startTime, 'ss,SSS').format("HH:mm:ss,SSS");
      instance.endTime = moment(startTime + 1, 'ss').format("HH:mm:ss,SSS");
      arrayOfSrt.push(instance);
    });
    return arrayOfSrt;

  };

  mod.convertToArray = function(data, key) {
    if (!_.has(data[0], key)) {
      return 'No key named ' + key;
    } else {
      return _.pluck(data, key);
    }
  }

  mod.getSrtArray = function(data, key) {
    var srtList = mod.convertToArray(data, key),
      srt = [];
    _.each(srtList, function(el, index) {
      var element = {
        name: data[index].imagefilename.substr(0, data[index].imagefilename.length - 4),
        srt: mod.convert(el)
      }
      mod.createFileFromObject(element);
      srt.push(element);
    })
    return srt;
  }

  mod.createFileFromObject = function(data) {
    var srt = parser.toSrt(data.srt);
    fs.writeFile("/tmp/" + data.name + ".srt", srt, function(err) {
      if (err) throw err;
    });
  }

  mod.main = function() {
    var filename = process.argv[2];
    var data = JSON.parse(fs.readFileSync(filename, { encoding: 'utf-8' }));
    var key = process.argv[3];
    mod.getSrtArray(data,key)
  }

  return mod;

})();

// ignore exports for browser
if (typeof exports === 'object') {
  module.exports = text2srt;
}

text2srt.main();<commit_msg>variable delays<commit_after>var _ = require('lodash'),
  moment = require('moment'),
  parser = require('subtitles-parser'),
  fs = require('fs');

var text2srt = (function() {
  var mod = {};

  mod.convert = function(data) {
    var srtObject = function() {
      this.id = '0';
      this.startTime = '00:00:00,000';
      this.endTime = '00:00:00,000';
      this.text = '';
    },
      arrayOfSrt = [];

    var srt = data.split("//");
    var currentTime = 0;
    _.each(srt, function(el, index) {
      var timeonscreen = 2.0 + (el.length)*0.02;
      if (el[el.length-2] == '.'){
        timeonscreen += 0.5;
      }
      var timebetweenlines = 0.002;
      var instance = new srtObject();
      instance.id = index + 1;
      instance.text = el;
      //var startTime = index * (timeonscreen + timebetweenlines);
      currentTime += timebetweenlines;
      var startTime = currentTime;
      instance.startTime = moment(startTime, 'ss,SSS').format("HH:mm:ss,SSS");
      instance.endTime = moment(startTime + timeonscreen, 'ss,SSS').format("HH:mm:ss,SSS");
      arrayOfSrt.push(instance);
      currentTime += timeonscreen;
    });
    return arrayOfSrt;

  };

  mod.convertToArray = function(data, key) {
    if (!_.has(data[0], key)) {
      return 'No key named ' + key;
    } else {
      return _.pluck(data, key);
    }
  }

  mod.getSrtArray = function(data, key) {
    var srtList = mod.convertToArray(data, key),
      srt = [];
    _.each(srtList, function(el, index) {
      var element = {
        name: data[index].imagefilename.substr(0, data[index].imagefilename.length - 4),
        srt: mod.convert(el)
      }
      mod.createFileFromObject(element);
      srt.push(element);
    })
    return srt;
  }

  mod.createFileFromObject = function(data) {
    var srt = parser.toSrt(data.srt);
    fs.writeFile("srt/" + data.name + ".txt", srt, function(err) {
      if (err) throw err;
    });
  }

  mod.main = function() {
    var filename = process.argv[2];
    var data = JSON.parse(fs.readFileSync(filename, { encoding: 'utf-8' }));
    var key = process.argv[3];
    mod.getSrtArray(data,key)
  }

  return mod;

})();

// ignore exports for browser
if (typeof exports === 'object') {
  module.exports = text2srt;
}

text2srt.main();
<|endoftext|>