Skip to content

Instantly share code, notes, and snippets.

View myndzi's full-sized avatar

Kris Reeves myndzi

  • Sigma Bold
  • Olympia, WA
View GitHub Profile
@myndzi
myndzi / .js
Created December 23, 2016 03:00
function getFoos(nextOffset, results) {
nextOffset = nextOffset || 0;
results = results || [ ];
return Promise.try(() => {
return getSomeFoos(nextOffset);
}).then(res => {
let newResults = results.concat(res.foos);
return res.hasMore ?
getFoos(res.nextOffset, newResults) :
// paginate('get', '/foo', {...})
const paginate = (method, path, opts, offset, results) => {
offset = offset || 0;
results = results || [ ];
return Promise.try(() =>
request(method, path, Object.assign(opts, { offset }))
).then(res => {
let newResults = results.concat(res.data);
if (!res.pagination) { return newResults; }
const Transform = require('stream').Transform;
const util = require('util');
function ChunkStream(options) {
if (!(this instanceof ChunkStream))
return new ChunkStream(options);
Transform.call(this, options);
this.chunkSize = options.chunkSize || 1500;
this._chunks = [ ];
function sequentially(array, promiseFn) {
return array.reduce((acc, current) =>
acc.then(results =>
promiseFn(current).then(res =>
results.concat(res)
)
)
, Promise.resolve([]);
}
penguinfrk: 2309,
Chew: 2236,
piper: 2215,
goonerJimmy: 2180,
Chopin: 2173,
Blitz: 2137,
JuhuarKnight: 2072,
shinanai: 2069,
be_f: 2032,
sonic: 2002,
// values 0 through 6 represent tetris pieces
// heap[1] is always the piece that has been seen the
// fewest number of times (or is tied with it)
heap = [unused, 0, 1, 2, 3, 4, 5, 6]
update(piece_id) {
// find the piece to update
for (ptr = 1; ptr <= 8; ptr++) {
if (heap[ptr] & 0x07 == piece_id) break;
}
if (ptr == 8) {
@myndzi
myndzi / .js
Last active May 14, 2017 20:03
var resource = app.swag.createResource('Foo');
resource.get('/foo', {
name: 'Foos',
params: {
filter: 'string',
itemsPerPage: 'integer'
},
returns: 'SomeModel' //models are declared elsewhere/separately
}, function (req, res) {
const schema = Joi.object().keys({
isRegister: Joi.any(),
username: Joi.string(),
password: Joi.string(),
email: Joi.string()
}).with('isRegister', 'username', 'password');
app.post('/register', function (req, res) {
const isValid = schema.validate(Object.assign({ isRegister: true }, req.params));
});
const Joi = require('joi');
const schema = {
a: Joi.number(),
b: Joi.number(),
};
const value = {
a: 'AAA',
b: "BBB"
@myndzi
myndzi / .js
Last active May 19, 2017 02:17
const Joi = require('joi');
const util = require('util');
const schema = Joi.object().keys({
a: Joi.number().meta('bad a'),
b: Joi.number().meta('bad b')
}).error(errs => {
let obj = { };
errs.forEach(err => {
obj[err.path] = Joi.reach(schema, err.path).describe().meta[0];