File size: 3,793 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
/*global console */
import * as is from '../is';
import * as strings from './strings';
import * as regex from './regex';
import * as sort from './sort';
import { memoize } from './memoize';
import { extend } from './extend';
export * from './colors';
export * from './maps';
export * from './strings';
export * from './timing';
export * from './hash';
export { strings, extend, extend as assign, memoize, regex, sort };
let warningsEnabled = true;
let warnSupported = console.warn != null; // eslint-disable-line no-console
let traceSupported = console.trace != null; // eslint-disable-line no-console
export const MAX_INT = Number.MAX_SAFE_INTEGER || 9007199254740991;
export const trueify = () => true;
export const falsify = () => false;
export const zeroify = () => 0;
export const noop = () => {};
export const error = msg => {
throw new Error( msg );
};
export const warnings = enabled => {
if( enabled !== undefined ){
warningsEnabled = !!enabled;
} else {
return warningsEnabled;
}
};
export const warn = msg => { /* eslint-disable no-console */
if( !warnings() ){ return; }
if( warnSupported ){
console.warn( msg );
} else {
console.log( msg );
if( traceSupported ){
console.trace();
}
}
}; /* eslint-enable */
export const clone = obj => {
return extend( {}, obj );
};
// gets a shallow copy of the argument
export const copy = obj => {
if( obj == null ){
return obj;
} if( is.array( obj ) ){
return obj.slice();
} else if( is.plainObject( obj ) ){
return clone( obj );
} else {
return obj;
}
};
export const copyArray = arr => {
return arr.slice();
};
export const clonePosition = pos => {
return { x: pos.x, y: pos.y };
};
export const uuid = ( a, b /* placeholders */) => {
for( // loop :)
b=a=''; // b - result , a - numeric letiable
a++<36; //
b+=a*51&52 // if "a" is not 9 or 14 or 19 or 24
? // return a random number or 4
(
a^15 // if "a" is not 15
? // generate a random number from 0 to 15
8^Math.random()*
(a^20?16:4) // unless "a" is 20, in which case a random number from 8 to 11
:
4 // otherwise 4
).toString(16)
:
'-' // in other cases (if "a" is 9,14,19,24) insert "-"
);
return b;
};
const _staticEmptyObject = {};
export const staticEmptyObject = () => _staticEmptyObject;
export const defaults = defaults => {
let keys = Object.keys( defaults );
return opts => {
let filledOpts = {};
for( let i = 0; i < keys.length; i++ ){
let key = keys[i];
let optVal = opts == null ? undefined : opts[key];
filledOpts[key] = optVal === undefined ? defaults[key] : optVal;
}
return filledOpts;
};
};
export const removeFromArray = ( arr, ele, oneCopy ) => {
for( let i = arr.length - 1; i >= 0; i-- ){
if( arr[i] === ele ){
arr.splice( i, 1 );
if( oneCopy ){ break; }
}
}
};
export const clearArray = arr => {
arr.splice( 0, arr.length );
};
export const push = ( arr, otherArr ) => {
for( let i = 0; i < otherArr.length; i++ ){
let el = otherArr[i];
arr.push( el );
}
};
export const getPrefixedProperty = ( obj, propName, prefix ) => {
if( prefix ){
propName = strings.prependCamel( prefix, propName ); // e.g. (labelWidth, source) => sourceLabelWidth
}
return obj[ propName ];
};
export const setPrefixedProperty = ( obj, propName, prefix, value ) => {
if( prefix ){
propName = strings.prependCamel( prefix, propName ); // e.g. (labelWidth, source) => sourceLabelWidth
}
obj[ propName ] = value;
};
|