tunnckoCore / blankr

:heart: tasks, todos, ideas, streaks, WIP, etc
https://i.am.charlike.online
4 stars 1 forks source link

next todos #24

Open tunnckoCore opened 9 years ago

tunnckoCore commented 9 years ago
var is = require('assert-kindof');

(a, c) => {
  is(a, 'string')
  is(c, 'function')
  // etc...
}

// instead of 

(a, c) => {
  if (typeof a === 'string') {throw new TypeError /*...*/}
  if (typeof c === 'function') {throw new TypeError /*...*/}
  // etc...
}
/**
 * Validate options object properties
 *
 * @param  {Object} `opts`
 * @return {Object}
 * @api private
 */
function validateOptions(opts) {
  var map = {
    query: 'so',
    path: 's',
    pathname: 's',
    host: 's',
    hostname: 's',
    timeout: 'n',
    socketTimeout: 'n',
    maxRedirects: 'n',
    followRedirects: 'b'
  };
  Object.keys(map).forEach(function(prop) {
    delegateError(opts[prop], map[prop], '[tosck] opts.' + prop);
  })
  return opts;
}

function delegateError(value, abbr, message) {
  var abbrevs = abbrev(abbr); // abbrev is `npm.im/map-types`
  if (abbrevs.length <= 1) {
    var type = abbrevs && abbrevs[0];
    if (value && kindOf(value) !== type) {
      // todo, should use beautify-errors here when they are rdy
      throw new TypeError(message + ' should be ' + type);
    }
    return;
  }
  abbrevs.forEach(function(type, i) {
    var next = abbrevs[i+1];
    if (value && kindOf(value) !== type && kindOf(value) !== next) {
      throw new TypeError(message + ' should be ' + type + ' or ' + next);
    }
  })
}

instead of doing

/**
 * Validate options object properties
 *
 * @param  {Object} `opts`
 * @return {Object}
 * @api private
 */
function validateOptions(opts) {
  if (opts.query && kindOf(opts.query) !== 'string' && kindOf(opts.query) !== 'object') {
    throw new TypeError('[tosck] opts.query should be string or object');
  }
  if (opts.path && kindOf(opts.path) !== 'string') {
    throw new TypeError('[tosck] opts.path should be string');
  }
  if (opts.pathname && kindOf(opts.pathname) !== 'string') {
    throw new TypeError('[tosck] opts.pathname should be string');
  }
  if (opts.host && kindOf(opts.host) !== 'string') {
    throw new TypeError('[tosck] opts.host should be string');
  }
  if (opts.hostname && kindOf(opts.hostname) !== 'string') {
    throw new TypeError('[tosck] opts.hostname should be string');
  }
  if (opts.timeout && kindOf(opts.timeout) !== 'number') {
    throw new TypeError('[tosck] opts.timeout should be number');
  }
  if (opts.socketTimeout && kindOf(opts.socketTimeout) !== 'number') {
    throw new TypeError('[tosck] opts.socketTimeout should be number');
  }
  if (opts.maxRedirects && kindOf(opts.maxRedirects) !== 'number') {
    throw new TypeError('[tosck] opts.maxRedirects should be number');
  }
  if (opts.followRedirects && kindOf(opts.followRedirects) !== 'boolean') {
    throw new TypeError('[tosck] opts.followRedirects should be boolean');
  }

  return opts;
}
var validate = require('abbrev-validation-map');
var opts = {
  path: 'foo/bar/baz',
  strict: true,
  plugins: ['one', 'two', 'three']
};
var map = {
  path: 's', // string
  strict: 'b', // boolean
  plugins: 'as' // array or string
};
validate(opts, map);
// pass
tunnckoCore commented 9 years ago

and because we are twins with jonschlinkert, he already made update-banner, lol

tunnckoCore commented 9 years ago

tricky (and I think it would be faster, because it is not based on regex)

trim-space-left and trim-space-right

var endsWith = require('ends-with');

function trickyTrimRight(str) {
  if (!endsWith(str, ' ')) {
    return str;
  }

  var i = 0;
  var j = str.length - 1;
  var c = '';

  while (i < j) {
    c += str[i];
    i++;
  }

  return endsWith(c, ' ') ? trickyTrimRight(c) : c;
}

function trickyTrimLeft(str) {
  if (!str.charAt(0) === ' ') {
    return str;
  }

  var i = 1;
  var j = str.length;
  var c = '';

  while (i < j) {
    c += str[i];
    i++;
  }

  return c.charAt(0) === ' ' ? trickyTrimLeft(c) : c;
}

and of course

console.log('!' + trickyTrimLeft('               unicorns'));
//=> '!unicrons'

console.log(trickyTrimRight('unicorns                    ') + '!');
//=> 'unicrons!'
tunnckoCore commented 9 years ago

stress-css

stress-css.js in 297bytes

(function(d,s,w,i)%7Bd=document,w=window,s=d.createElement('script'),i=function()%7Bif(w.stressTest)%7Bw.stressTest.bookmarklet()%7Delse%7BsetTimeout(i,100)%7D%7D;s.src='http://j.mp/1PPjPmC?_='%2BMath.random();(d.body%7C%7Cd.getElementsByTagName('head')%5B0%5D).appendChild(s);i();%7D)();
tunnckoCore commented 9 years ago

one-liner for downloading all statics files from website (static blogs), scripts, styles and etc - exported in directory

wget --random-wait -r -p -e robots=off http://jenius.github.io/rupture/