Created
March 15, 2014 02:10
-
-
Save 11111000000/9560857 to your computer and use it in GitHub Desktop.
Some livescript functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window <<<< | |
isGradient: -> (it |> /^\s*([a-z]+)\-gradient\(\s*(.*)\s*\)\s*$/gmi.exec)?[1] in <[ linear radial ]> | |
toFixedOrInt: (precision, value)--> | |
a={} | |
power = 10 `Math.pow` (precision or 0) | |
value = Math.round(value * power) / power | |
rounded = value |> Math.round | |
if rounded is value then value = rounded | |
value |> String | |
toFixed: (precision, value)--> | |
power = 10 `Math.pow` precision or 0 | |
(Math.round(value * power) / power) |> String | |
compare: (f, x, y) --> | |
| (f x) > (f y) => 1 | |
| (f x) < (f y) => -1 | |
| otherwise => 0 | |
prop: (obj, path)--> | |
prop=obj | |
each path.split \., (o) -> | |
if typeof! prop[o] is \Function then prop[o]() else prop = prop[o] | |
prop | |
normalizeEvent: (event)-> | |
if not event.offsetX? | |
event <<<< | |
offsetX: event.pageX - $(event.target).offset!.left | |
offsetY: event.pageY - $(event.target).offset!.top | |
event | |
toDot: (obj, base = '')-> | |
result = {} | |
if typeof! obj isnt \Object | |
result[base] = obj | |
else | |
for key,val of obj | |
result <<<< toDot val, "#base#{ if base then '.' else '' }#key" | |
result | |
toDash: (camel)-> camel.replace /([A-Z])/g, (letter)-> "-#{letter.toLowerCase!}" | |
expand: (something)-> | |
if something |> $.isArray | |
something |> Obj.map -> expand it | |
else | |
obj = {} | |
$.each something, (dotpath,value)-> | |
path = ("#{dotpath}".split \.) |> reverse | |
nested = obj | |
while (key = path.pop!) | |
nested = nested[key] = | |
if path.length then | |
if nested[key]? and typeof! nested[key] isnt \Object | |
console.log \expand:TYPE_WARNING:, something, key, nested[key] | |
nested[key] or {} | |
else | |
if typeof! value is \Object | |
result = value |> expand | |
else | |
value | |
obj | |
listTo: (fn, arr=[]) --> arr |> (map fn) |> listToObj | |
between: (a,b,val) --> minimum [b, maximum [a, val]] | |
indexIf: (arr,func) --> | |
for i, el of arr | |
if func(el) | |
return i | |
return -1 | |
deepClone: -> | |
if typeof! it isnt \Object | |
return it | |
$.extend on, {}, it | |
deepFilter: (f, xs) --> | |
f = objToFunc f if typeof! f isnt \Function | |
if typeof! xs is \Object | |
{ [key, (deepfilter f, x)] for key, x of xs when (typeof! x is \Object) or f x } | |
else | |
xs | |
#result = [x for x in xs when f x] | |
#if type is \String then result * '' else result | |
isTextSelected: (input)-> | |
startPos = input.selectionStart | |
endPos = input.selectionEnd | |
doc = document.selection | |
if doc and doc.createRange!.text.length isnt 0 | |
return yes | |
else if not doc and ( input?.value?.substring startPos, endPos ).length isnt 0 | |
return yes | |
no | |
strDiff: (a,b)--> | |
result = '' | |
for x,i in a | |
if a[i] != b[i] | |
result += a[i] | |
result | |
arrDiff: (a, b)--> | |
| not a.length or not b.length => {A : a, B : b} | |
| a.length is b.length is 0 => no | |
| _ => [ diff for i from 0 til maximum [ a.length, b.length ] | diff = objDiff(a[i], b[i]) ] | |
deepFilterEmptyObj: (obj)-> | |
#if TEST => | |
#console.log \obj==, obj | |
#console.log ( not obj? or (typeof! obj isnt \Object)) | |
#console.log ( Obj.empty obj ) | |
a= | |
| not obj? or (typeof! obj isnt \Object) => obj | |
| Obj.empty obj => {} | |
| otherwise => {[key, val] for key,val of obj | |
| ( (typeof! val isnt \Object) or | |
not (Obj.empty deepFilterEmptyObj val) ) } | |
objDiff: (A, B)--> | |
recurse = (A,B)-> | |
| (not A?) and (not B?) => no | |
| (typeof! A) isnt (typeof! B) => { A : A, B : B } | |
| (typeof! A) is \Array => arrDiff A, B | |
| (typeof! A) is \Object => { [ key, diff ] for key, val of A | diff = (recurse val, B[key]) } | |
| A isnt B => "#{A} -> #{B}" | |
| otherwise => {} | |
result = recurse(A,B) | |
a = if result.A? or result.B? | |
result.A = (result.A or {} ) |> deepFilterEmptyObj |> toDot | |
result.B = (result.B or {} ) |> deepFilterEmptyObj |> toDot | |
else | |
result |> deepFilterEmptyObj |> toDot | |
#objDiff: (A, B, debug)-> | |
#recurse = (A,B)-> | |
#_= | |
#| (not A?) or (not B?) => {} | |
#| (typeof! A) isnt (typeof! B) => { 'A' : A, 'B' : B } | |
#| (typeof! A) is \Array => arrDiff A, B | |
#| (typeof! A) is \Object => { [ key, diff ] for key, val of A when diff = recurse val, B[key] } | |
#| A isnt B => "#{A} -> #{B}" | |
#| _ => {} | |
#result = recurse(A,B) | |
#result.A = (result.A |> deepFilterEmptyObj |> toDot) if result.A? | |
#result.B = (result.B |> deepFilterEmptyObj |> toDot) if result.B? | |
#result | |
flat-with-keys: -> | |
recurse = -> | |
| it |> is-type \Object => it |> obj-to-pairs |> map -> [it.0, (it.1 |> recurse)] | |
| _ => it | |
it |> recurse |> flatten | |
find-path-of: (something, object)--> | |
recurse = (place)-> | |
_= | |
| place is something => [ true ] | |
| place |> is-type \Object => [ [k, path] for k, v of place when (path = recurse(v))?.length > 0 ] | |
| place |> is-type \Array => [ [i, path] for i, v in place when (path = recurse(v))?.length > 0 ] | |
| _ => false | |
object |> recurse | |
find-path-of-val-or-key: (something, object)--> | |
recurse = (place)-> | |
_= | |
| place is something => [ true ] | |
| place |> is-type \Object => do -> | |
| (something in (place |> keys)) => [ true ] | |
| _ => [ [k, path] for k, v of place when (path = recurse(v))?.length > 0 ] | |
| place |> is-type \Array => [ i for v, i in place when (path = recurse(v))?.length > 0 ] | |
| _ => false | |
object |> recurse | |
filter-by-key: (fn, object) --> { [key, object[key]] for key in keys(object) when fn(key) } | |
in-deep: (target, something) --> | |
| something is target => true | |
| something |> is-type \Array => something |> any -> target `in-deep` it | |
| something |> is-type \Object => something |> values |> any -> target `in-deep` it | |
| otherwise => no | |
find-root: (target, object) --> | |
return if typeof! object isnt \Object | |
for key, val of object | |
return key if target `in-deep` val | |
return void |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment