Skip to content

Instantly share code, notes, and snippets.

@bga
bga / [fun] various forms of ternary operator in JavaScript.js
Created March 7, 2012 15:10
[fun] various forms of ternary operator in JavaScript
// add your variants of
a ? b : c
// in comments :)
// a is boolean
// b and c - any type
// lazy evaluation isnt important
@bga
bga / z.js
Created February 27, 2012 12:31 — forked from heapwolf/z.js
z
var match = []
var _finalizeExpr = function(match) {
var vs = match[0]
var _iter = match[1]
var _done = match[2]
var i = 0
var _iterWrappper = function() {
if(i === vs.length) {
@bga
bga / basic.js
Created December 18, 2011 13:12
basic.js
/*
case insensitive names!
*/
var Basic = function(obj) {
var _transform = (function(){
var cache = Object.create(null)
return function(name){
return (cache[name] // lisp!
|| (cache[name] =
@bga
bga / breakable generic _each.js
Created November 5, 2011 23:08
breakable generic _each.js
/*
Generic breakable _each
*/
Object.prototype._each = function(_fn) {
for(var i in this) {
if(!{}.hasOwnProperty.call(this, i))
continue
var ret = _fn(this[i], i, this)
if(ret != null)
@bga
bga / ltml.js
Created November 1, 2011 23:53
ltml.js
String.prototype._quoteCString = #{
-> @replace(/\\/g, '\\\\').replace(/\"/g, '\\"')
}
fix _ltmlToHtml = #(s) {
fix _skipSpaces = #(p) {
-> p + s.slice(p).match(/^\s*/)[0].length // skip
}
fix _passAttr = #(p) {
switch(s.charAt(p))
@bga
bga / declarative style evaluation.js
Created September 22, 2011 13:09
declarative style evaluation.js
/*
Declarative style evaluation.
Advantages
order of evaluation is not important
evaluation is lazy i.e. its evaluated only if required
auto memorization of evaluation (only for pure fns)
expressions are writed as in math, no mutable values
*/
Object.prototype._let = #(name, _calc) {
/*
Very simple. Uses [gs]etters and caller. Precache '_method in Class' relations using unique `Class.jbId_` and `_method.jbOwnerClassIdMap_`.
*/
(function()
{
var nextClassId = 1;
var _methodInClass = function(_fn, Class)
{
var classId = Class.jbId_;
/*
`String.prototype._matchWithPoss(re, p)` (c) New BSD License
@param re {RegExp}
@param p ?= 0 {Number} start position to match
Matches regExp with subgroups and back references as build-in `String.prototype.match` but adds info about subgroups positions in string.
Example:
console.log(' 123abcABC123abc'._matchWithPoss(/(?:\d+)([a-z]+)[A-Z]+(\d+\1)/, 1));
[
{ // 0 - whole matched substring with matched position
"s": "123abcABC123abc",
/* Reordered binary search and cache-misses. Thanks for idea, http://www.linux.org.ru/people/ckotinko/profile. */
var n = 800000;
var ENABLE_TYPED_ARRAYS = 1;
var MAKE_SOLID = 1;
var $G = this;
var MyArray =
ENABLE_TYPED_ARRAYS && (
var _copyFile = function(
iName,
oName,
_ret
)
{
var CHUNK_SIZE = 65536;
var _copy = function(iF, oF, size, _ret)