๐นhelpers
global functions
Array
arr.isMatrix - ๅฏ็จไพๆชขๆฅ 2D data ่ณๆๆ ผๅผๆฏๅฆๆญฃ็ขบ
arr.indexDictionaryForValues() - ๆพๆฌไฝ็ดขๅผ (column index)
arr.containsSubarray() - ๆชขๆฅๆๅๆฏๅฆๅ ๅซ็นๅฎๆฌไฝๅ็จฑ
const { keys } = Object;
const {log} = Logger;
/**
* โญ global functions:
* --------------------
* - typeName()
*
* โญ Array
* --------------------
* - sum()
* - average()
* - padEndSpaces(n)
* - rank()
* - indexDictionaryForValues()
* - containsSubarray()
* - removeDuplicates()
* - uniqueMerge()
* - isMatrix
*
* โญ String
* --------------------
* - removeWhitespaces()
*/
// โโโโโโโโโโโโโโ
// โ Global Functions โ
// โโโโโโโโโโโโโโ
// ๐ธ value's type
function typeName(value) {
// 01234567โญโโโฎโฑ -1
// "[object XXXX]"
// ^^^^ <---- slice(8, -1)
return Object.prototype.toString.call(value).slice(8, -1);
}
// โโโโโโโโ
// โ Array โ
// โโโโโโโโ
// ๐ธ arr.sum()
Array.prototype.sum = function(){
return this.reduce((result, value) => result + value, 0);
};
// ๐ธ arr.average()
Array.prototype.average = function(){
return this.sum() / this.length;
};
// ๐ธ arr.padEndSpaces(n)
/**
* pad spaces ("") to the end of array
*
* @example
*
* const a = ['็ญ็ด','ๅบง่','ๅงๅ'];
* a.padEndSpaces(3), // [ '็ญ็ด', 'ๅบง่', 'ๅงๅ', '', '', '' ]
*
*/
Array.prototype.padEndSpaces = function(n) {
return this.concat(Array(n).fill(''));
};
// rank objects
/**
* rank array of objects
*
* @example
*
* students.rank('rankOfSchool', (a, b) => +b.total - +a.total);
*
*/
Array.prototype.rank = function(rankPropertyName, compare){
const sorted = this.sort(compare);
let current; // current object keeping the current rank
// ๐งจ ้ทๅ๏ผ
// - indices (keys) of array.entries() are "integer-based"
// - indices (keys) of Object.entries(array) are "string-based"โโโ
for(const [i, obj] of sorted.entries()){
// ๅฆๆไธๅๅ๏ผๅฐฑ็ข็ๆฐ็็ญๆๅๆฌก๏ผไธฆ่จ้็ฎๅ็็ธฝๅ่็ญๆๅๆฌก
if (!current || compare(current, obj) !== 0) {
obj[rankPropertyName] = i + 1;
current = obj;
}
// ๅฆๆๅๅ๏ผๅฐฑ็จ็ธๅ็็ญๆใ
else {
obj[rankPropertyName] = current[rankPropertyName];
}
}
return this; // for chaining
};
/*
* ๐ธ array.indexDictionaryForValues(values)
* return the index for each value (a dictionary object)
*
* @example
*
* let row = ["ๅบง่", "ๅงๅ", "ๅ่ชๆ", "่ฑ่ชๆ"];
* row.indexDictionaryForValues(["ๅบง่", "ๅงๅ"]) // { 'ๅบง่': 0, 'ๅงๅ': 1 }
*/
Array.prototype.indexDictionaryForValues = function(values){
let dict = {};
for(const value of values){
const i = this.indexOf(value);
if(i < 0) throw new Error(`โ array.indexDictionaryForValues(): array doesn't have value "${value}"`);
dict[value] = i;
}
return dict;
};
// ๐ธ arr.containsSubarray()
Array.prototype.containsSubarray = function(subarray, orderIsRelevant=false) {
// โญ closure with internal parameter `i`
const valueIsInArraySequentially = (i => v => i = this.indexOf(v, i) + 1)(0);
const valueIsInArray = (v => this.includes(v));
const valuePassedTest = orderIsRelevant ? valueIsInArraySequentially : valueIsInArray;
return subarray.every(valuePassedTest);
};
// ๐ธ array.removeDuplicates()
Array.prototype.removeDuplicates = function(){
return [... new Set(this)]
};
// ๐ธ array.uniqueMerge()
Array.prototype.uniqueMerge = function(){
return this.flat().removeDuplicates();
};
// ๐ธ arr.isMatrix
Object.defineProperty(Array.prototype, 'isMatrix', {
get(){
if (!this[0]) {
console.log(`โน๏ธ array.isMatrix: array doesn't have first row. `);
return false;
}
function valueIsArray(value, i){
let isArray = Array.isArray(value);
if (!isArray) {
console.log(`โน๏ธ array.isMatrix: array[${i}] is not an array.`);
return false;
}
return true;
}
// check if every value is an array.
if (!this.every((value, i) => valueIsArray(value, i))) return false;
// check if first row has elements
if (!this[0].length > 0) {
console.log(`โน๏ธ array.isMatrix: first row has no values.`);
return false;
}
const arraySameLength = (array, i) => {
const len = array.length;
const len0 = this[0].length;
if (len !== len0){
console.log(`โน๏ธ array.isMatrix: array[${i}].length (${len}) โ array[0].length (${len0}).`);
return false
}
return true;
}
// check if every array has the same length;
if (!this.every((arr, i) => arraySameLength(arr, i))) return false;
return true;
}
});
// โโโโโโโโ
// โ String โ
// โโโโโโโโ
// ๐ธ str.removeWhitespaces()
String.prototype.removeWhitespaces = function() {
return this.replace(/\s+/g, '');
};
object getter/setter
Last updated