0.2.1 • Published 9 years ago

kondico v0.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
9 years ago

Kondico ===

Functional conditions and compositions (uses Formi).

###Status

Codeship Status for krambuhl/Kondico

Code Climate

Test Coverage

API Docs

Kondico exposes the Kondico function.

  • Kondico(boolean, [options]) -> Function
  • Kondico(func, [options]) -> Function

####Kondico(boolean, options)

Defines function that returns input boolean.

Example

var isTrue = Kondico(true);
isTrue(); // ==> true

####Kondico(func, options)

Defines function that returns result of a called function.

Example

var isDefined = Kondico(function(val) { return val !== undefined;  }); 
isDefined(640) // true
isDefined(undefined) // false

###Kondico Options

OptionValueDescription
onceBooleanOnly run function body once, cache result
memoizeBoolean/FunctionOnly run function body once per input value

####Kondico(func, { once: true })

Defines a function that runs it's body a single time, any subsequent calls will return that same value without running that function body.

Example

var hasJquery = Kondico(function() {
    return typeof jQuery === 'function';
}, { once: true });

if (hasJquery()) {
    $('html').addClass('has-jquery');
    alert('has jquery: ' + hasJquery());
}

####Kondico(func, { memoize: true })

Defines a function that runs it's body a single time for each unique input, any subsequent calls with the same input will return the same value without running the function body.

memoize option can be a boolean value or a hashFunction that returns an id used to cache a functions return; hashFunction will return first argument by default.

Example

var has = Kondico(function(val) {
    return typeof val === 'function';
}, { memoize: true });

if (has(jQuery)) {
    $('html').addClass('has-jquery');
    alert('has jquery: ' + has(jQuery));
}

##Compositions (Logic Gates)

Kondico defines logic gate function for composing more complex conditions. All functions will operate on any number of arguments. not returns an array if more than one argument is passed.

  • Kondico.not
  • Kondico.or
  • Kondico.and
  • Kondico.nor
  • Kondico.nand
  • Kondico.xor
  • Kondico.xnor

Example Useages

####Media Queries

var smallSize = Kondico(function(val) { return val < 720;  }); 
var largeSize = Kondico(function(val) { return val > 1280; });
var mediumSize = Kondico.nor(smallSize, largeSize);

// example window size (960x720)
smallSize(window.innerWidth) // false
mediumSize(window.innerWidth) // true
largeSize(window.innerWidth) // false

####Feature Detection

var hasCanvas = Kondico(function() {
    var elem = createElement('canvas');
    return !!(elem.getContext && elem.getContext('2d'));
}, { once: true });