@deepauto/ffcreatorlite
Version:
FFCreatorLite is a lightweight and flexible short video production library
201 lines (174 loc) • 4.74 kB
JavaScript
;
/**
* Utils - Utility function collection
*
* ####Example:
*
* const effect = Utils.mergeExclude(effect, conf, ["type"]);
* const id = Utils.genId(this.type);
*
* @object
*/
const forEach = require('lodash.foreach');
const cache = {};
const Utils = {
/**
* Generate auto-increment id based on type
* @param {string} type - type
* @return {string} id xxxx_10
* @public
*/
genId(type) {
if (!cache[type]) cache[type] = 1;
return type + String(cache[type]++);
},
/**
* Generate 24-bit random number
* @return {string} uid adsfUsdfn2
* @public
*/
uid() {
return Math.random().toString(36).substr(-8) + Math.random().toString(36).substr(-8);
},
getFormatTime(dataTime = 0) {
const now = dataTime ? new Date(dataTime) : new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始,需+1
const day = String(now.getDate()).padStart(2, '0');
const hours = String(now.getHours()).padStart(2, '0');
const minutes = String(now.getMinutes()).padStart(2, '0');
const seconds = String(now.getSeconds()).padStart(2, '0');
return `${year}${month}${day}${hours}${minutes}${seconds}`;
},
//'00:00:01,500' 转成毫秒 1500
timeToSeconds(time) {
const parts = time.split(',');
const seconds = parts[0].split(':');
const hours = parseInt(seconds[0], 10);
const minutes = parseInt(seconds[1], 10);
const sec = parseInt(seconds[2], 10);
const millis = parseInt(parts[1], 10);
return (hours * 3600 + minutes * 60 + sec) * 1000 + millis;
},
/**
* Delete an element of the array
* @param {array} arr - target array
* @param {any} elem - an element
* @return {array} Original array
* @public
*/
deleteArrayElement(arr, elem) {
const index = arr.indexOf(elem);
if (index > -1) arr.splice(index, 1);
return arr;
},
/**
* Swap two elements of an array
* @param {array} arr - target array
* @param {any} elem1 - an element
* @param {any} elem1 - other element
* @return {array} Original array
* @public
*/
swapArrayElement(arr, elem1, elem2) {
const index1 = typeof elem1 === 'number' ? elem1 : arr.indexOf(elem1);
const index2 = typeof elem2 === 'number' ? elem2 : arr.indexOf(elem2);
const temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
return arr;
},
/**
* Sort the array according to a certain key
* @public
*/
sortArrayByKey(arr, key, val) {
const carr = [];
forEach(arr, elem => {
if (elem[key] === val) {
carr.unshift(elem);
} else {
carr.push(elem);
}
});
return carr;
},
/**
* Remove undefined empty elements
* @param {object} obj - target object
* @return {object} target object
* @public
*/
deleteUndefined(obj) {
for (let key in obj) {
if (obj[key] === undefined) {
delete obj[key];
}
}
return obj;
},
floor(n, s = 2) {
const k = Math.pow(10, s);
return Math.floor(n * k) / k;
},
floorObject(obj, s = 2) {
for (let key in obj) {
Utils.floor(obj[key], s);
}
return obj;
},
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
/**
* Destroy the elements in object
* @param {object} obj - target object
* @public
*/
destroyObj(obj) {
if (typeof obj !== 'object') return;
for (let key in obj) {
delete obj[key];
}
},
angleToRadian(angle, s = 4) {
return this.floor((angle / 180) * Math.PI, s);
},
angleToPI(angle, s = 4) {
const pi = this.angleToRadian(angle, s);
return `${pi}*PI`;
},
replacePlusMinus(str) {
return str
.replace(/\+\-/gi, '-')
.replace(/\-\+/gi, '-')
.replace(/\-\-/gi, '+')
.replace(/\+\+/gi, '+')
.replace(/\(t\-0\)/gi, 't')
.replace(/\(on\-0\)/gi, 'on');
},
/**
* Fix wrong file directory path // -> /
* @param {string} path - file directory path
* @public
*/
fixFolderPath(path) {
return path.replace(/\/\//gi, '/');
},
explan(json) {
try {
return JSON.stringify(json);
} catch (e) {
return json.error;
}
},
excludeBind(obj1, obj2 = {}, exclude = []) {
for (let key in obj2) {
if (exclude.indexOf(key) < 0 && obj2[key] !== undefined) obj1[key] = obj2[key];
}
},
clone(obj) {
return Object.assign({}, obj);
},
};
module.exports = Utils;