| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- function p5Utils__format(str, args) {
- return args.reduce(
- function (ret, arg, idx) {
- return ret.replace(new RegExp('\\{' + idx + '\\}', 'gm'), arg)
- },
- str
- )
- }
- function p5Utils__parseFloatOrZero(strToParse) {
- if (!strToParse) return 0
- if ("string" === typeof strToParse) {
- strToParse = strToParse.replace(/,/g, '.')
- strToParse = strToParse.replace(/ /g, '')
- }
- var floatVal = parseFloat(strToParse)
- return (!isNaN(floatVal))? floatVal : 0
- }
- function p5Utils__pricePrint(value, defaultValue) {
- return p5Utils__pricePrintFormat(value, defaultValue, '.', ',');
- }
- function p5Utils__pricePrintPL(value, defaultValue) {
- return p5Utils__pricePrintFormat(value, defaultValue, ',', ' ');
- }
- function p5Utils__pricePrintFormat(value, defaultValue, decPoint, thousands_sep) {
- if (undefined === defaultValue) defaultValue = '';
- if (!value && value !== 0 && value !== '0') return defaultValue;
- if ("number" === typeof value) {
- } else if ("string" === typeof value) {
- value = p5Utils__parseFloatOrZero(value);
- if (isNaN(value)) {
- return defaultValue;
- }
- } else {
- return defaultValue;
- }
- var valueParts = value.toFixed(2).split('.'),
- firstPart = valueParts[0],
- secondPart = valueParts[1],
- parts = [],
- minus = ''
- ;
- if ('-' === firstPart.substr(0, 1)) {
- minus = '-';
- firstPart = firstPart.substr(1);
- }
- var len = firstPart.length,
- str = firstPart,
- i = 0
- ;
- while (len > 3) {
- parts.unshift(str.substr(-3));
- len -= 3;
- str = str.substr(0, len);
- i++;
- }
- if (len > 0) {
- parts.unshift(str);
- }
- return '' + minus + parts.join(thousands_sep) + decPoint + secondPart;
- }
- function p5Utils__clearNode(node) {
- if (!node || !node.lastChild) return;
- var lastNode;
- while (lastNode = node.lastChild) node.removeChild(lastNode);
- }
- function p5Utils__nodeStyle(node, style) {
- if (!node || !style) return;
- for (i in style) node.style[i] = style[i];
- }
- function p5Utils__promisify(fun) {
- return function () {
- var args = new Array(arguments.length);
- for (var i = 0; i < args.length; ++i) args[i] = arguments[i];
- return new Promise(function (resolve, reject) {
- fun.apply(fun, [].concat(args, function (err, res) {
- return (err) ? reject(err) : resolve(res)
- }))
- })
- }
- }
- // Object.prototype.toString.call([]) // "[object Array]"
- // Object.prototype.toString.call('') // "[object String]"
- // Object.prototype.toString.call({}) // "[object Object]"
- // Object.prototype.toString.call(null) // "[object Null]"
- // Object.prototype.toString.call() // "[object Undefined]"
- function p5Utils__isString(arg) { return ('string' === typeof arg); }// '[object String]' === Object.prototype.toString.call(arg); }
- function p5Utils__isArray(arg) { return (Array.isArray) ? Array.isArray(arg) : '[object Array]' === Object.prototype.toString.call(arg); }
- function p5Utils__isObject(arg) { return ('object' === typeof arg && null !== arg && 'function' === typeof arg.constructor && !p5Utils__isArray(arg)); } // '[object Object]' === Object.prototype.toString.call(arg); }
- global.p5Utils__format = p5Utils__format
- global.p5Utils__parseFloatOrZero = p5Utils__parseFloatOrZero
- global.p5Utils__pricePrint = p5Utils__pricePrint
- global.p5Utils__pricePrintPL = p5Utils__pricePrintPL
- global.p5Utils__pricePrintFormat = p5Utils__pricePrintFormat
- global.p5Utils__clearNode = p5Utils__clearNode
- global.p5Utils__nodeStyle = p5Utils__nodeStyle
- global.p5Utils__promisify = p5Utils__promisify
- global.p5Utils__isString = p5Utils__isString
- global.p5Utils__isArray = p5Utils__isArray
- global.p5Utils__isObject = p5Utils__isObject
|