65 lines
1.7 KiB
Plaintext
65 lines
1.7 KiB
Plaintext
// @flow weak
|
|
|
|
import warning from 'warning';
|
|
|
|
export function capitalize(string) {
|
|
if (process.env.NODE_ENV !== 'production' && typeof string !== 'string') {
|
|
throw new Error('Material-UI: capitalize(string) expects a string argument.');
|
|
}
|
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
|
}
|
|
|
|
export function contains(obj: Object, pred: Object) {
|
|
return Object.keys(pred).every(key => {
|
|
return obj.hasOwnProperty(key) && obj[key] === pred[key];
|
|
});
|
|
}
|
|
|
|
export function findIndex(arr: Array<any>, pred: any) {
|
|
const predType = typeof pred;
|
|
for (let i = 0; i < arr.length; i += 1) {
|
|
if (predType === 'function' && !!pred(arr[i], i, arr) === true) {
|
|
return i;
|
|
}
|
|
if (predType === 'object' && contains(arr[i], pred)) {
|
|
return i;
|
|
}
|
|
if (['string', 'number', 'boolean'].indexOf(predType) !== -1) {
|
|
return arr.indexOf(pred);
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
export function find(arr: Array<any>, pred: any) {
|
|
const index = findIndex(arr, pred);
|
|
return index > -1 ? arr[index] : undefined;
|
|
}
|
|
|
|
/**
|
|
* Safe chained function
|
|
*
|
|
* Will only create a new function if needed,
|
|
* otherwise will pass back existing functions or null.
|
|
*
|
|
* @param {function} functions to chain
|
|
* @returns {function|null}
|
|
*/
|
|
export function createChainedFunction(...funcs: Array<any>) {
|
|
return funcs.filter(func => func != null).reduce(
|
|
(acc, func) => {
|
|
warning(
|
|
typeof func === 'function',
|
|
'Material-UI: invalid Argument Type, must only provide functions, undefined, or null.',
|
|
);
|
|
|
|
return function chainedFunction(...args) {
|
|
acc.apply(this, args);
|
|
func.apply(this, args);
|
|
};
|
|
},
|
|
() => {},
|
|
);
|
|
}
|