Function::property = (prop, desc) ->
if desc instanceof Function
desc = get: desc
Object.defineProperty @prototype, prop, desc
This file is part of the Mixco framework.
- View me on a static web
- View me on GitHub
This module contains a series of utility functions.
We provide a property class method to define properties — i.e. attributes that are accessed via setters and getters.
Function::property = (prop, desc) ->
if desc instanceof Function
desc = get: desc
Object.defineProperty @prototype, prop, desc
Number::clamp = (min, max) -> Math.min Math.max(this, min), max
Number::sign = () -> if this < 0 then -1 else 1
Array::equals = (other) ->
@length is other.length and @every (elem, i) -> elem is other[i]
exports.copy = (a, b) ->
b ?= {}
for k, v of a
b[k] = v
b
This can be used to guard against any possible exception, printing an error on the console when it happens. This is specially useful in the context of Mixxx.
exports.catching = (f) -> ->
try
f.apply @, arguments
catch err
console.log "ERROR: #{err}"
Throws an error if value is false. The error can be a custom string.
exports.assert = (value, error=undefined) ->
if not value
throw new Error(if error? then error else "Assertion failed")
exports.xmlEscape = (str) ->
str?.replace('&', '&')
.replace('"', '"')
.replace('>', '>')
.replace('<', '<') ? ''
Generates a string that contains depth number of spaces.
exports.indent = (depth) ->
Array(depth*4).join(" ")
Generates a string with a C-style hexadecimal representation of a number.
exports.hexStr = (number) ->
"0x#{number.toString 16}"
Generates a XML tag with the passed in value or nothing.
exports.xmlTag = (str, value, indent=0) ->
if value?
"#{exports.indent indent}<#{str}>#{value}</#{str}>"
else
""
Joins several lines, removing empty ones.
exports.joinLn = (lines) ->
lines.filter((x) -> x).join('\n')
Generates a function that constructs an object of type Klass,
forwarding all its parameters to the constructor. I hate using the
new
operator, so this will be used on most exported classes.
exports.factory = (Klass) -> -> new Klass arguments...
Copyright (C) 2013 Juan Pedro Bolívar Puente
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.