codecombat/app/lib/utils.coffee

100 lines
3.1 KiB
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
module.exports.clone = (obj) ->
2014-06-30 22:16:26 -04:00
return obj if obj is null or typeof (obj) isnt 'object'
2014-01-03 13:32:13 -05:00
temp = obj.constructor()
for key of obj
temp[key] = module.exports.clone(obj[key])
temp
module.exports.combineAncestralObject = (obj, propertyName) ->
combined = {}
while obj?[propertyName]
for key, value of obj[propertyName]
continue if combined[key]
combined[key] = value
if obj.__proto__
obj = obj.__proto__
else
# IE has no __proto__. TODO: does this even work? At most it doesn't crash.
obj = Object.getPrototypeOf(obj)
combined
module.exports.normalizeFunc = (func_thing, object) ->
# func could be a string to a function in this class
# or a function in its own right
object ?= {}
if _.isString(func_thing)
func = object[func_thing]
if not func
2014-07-04 10:23:47 -04:00
console.error "Could not find method #{func_thing} in object #{@}"
2014-01-03 13:32:13 -05:00
return => null # always return a func, or Mediator will go boom
func_thing = func
2014-03-13 11:39:53 -04:00
return func_thing
2014-01-10 19:48:28 -05:00
module.exports.hexToHSL = (hex) ->
rgbToHsl(hexToR(hex), hexToG(hex), hexToB(hex))
hexToR = (h) -> parseInt (cutHex(h)).substring(0, 2), 16
hexToG = (h) -> parseInt (cutHex(h)).substring(2, 4), 16
hexToB = (h) -> parseInt (cutHex(h)).substring(4, 6), 16
2014-06-30 22:16:26 -04:00
cutHex = (h) -> (if (h.charAt(0) is '#') then h.substring(1, 7) else h)
2014-03-13 11:39:53 -04:00
2014-01-10 19:48:28 -05:00
module.exports.hslToHex = (hsl) ->
'#' + (toHex(n) for n in hslToRgb(hsl...)).join('')
2014-03-13 11:39:53 -04:00
2014-01-10 19:48:28 -05:00
toHex = (n) ->
h = Math.floor(n).toString(16)
h = '0'+h if h.length is 1
2014-03-12 15:30:43 -04:00
h
2014-03-12 17:48:27 -04:00
module.exports.i18n = (say, target, language=me.lang(), fallback='en') ->
2014-03-12 15:30:43 -04:00
generalResult = null
fallbackResult = null
fallforwardResult = null # If a general language isn't available, the first specific one will do
matches = (/\w+/gi).exec(language)
generalName = matches[0] if matches
2014-03-12 19:47:08 -04:00
for localeName, locale of say.i18n
if target of locale
result = locale[target]
2014-03-12 15:30:43 -04:00
else continue
2014-07-04 10:23:47 -04:00
return result if localeName is language
generalResult = result if localeName is generalName
fallbackResult = result if localeName is fallback
fallforwardResult = result if localeName.indexOf(language) is 0 and not fallforwardResult?
2014-03-12 15:30:43 -04:00
return generalResult if generalResult?
return fallforwardResult if fallforwardResult?
2014-03-12 19:47:08 -04:00
return fallbackResult if fallbackResult?
return say[target] if target of say
2014-03-13 11:39:53 -04:00
null
2014-05-18 15:14:22 -04:00
module.exports.getByPath = (target, path) ->
pieces = path.split('.')
obj = target
for piece in pieces
return undefined unless piece of obj
obj = obj[piece]
obj
module.exports.round = _.curry (digits, n) ->
n = +n.toFixed(digits)
2014-06-12 13:39:45 -04:00
positify = (func) -> (x) -> if x > 0 then func(x) else 0
# f(x) = ax + b
createLinearFunc = (params) ->
(x) -> (params.a or 1) * x + (params.b or 0)
2014-06-12 13:39:45 -04:00
# f(x) = ax² + bx + c
createQuadraticFunc = (params) ->
(x) -> (params.a or 1) * x * x + (params.b or 1) * x + (params.c or 0)
# f(x) = a log(b (x + c)) + d
createLogFunc = (params) ->
(x) -> if x > 0 then (params.a or 1) * Math.log((params.b or 1) * (x + (params.c or 0))) + (params.d or 0) else 0
module.exports.functionCreators =
linear: positify(createLinearFunc)
quadratic: positify(createQuadraticFunc)
logarithmic: positify(createLogFunc)