Skip to content

Commit

Permalink
Add js static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kekefreedog committed Aug 9, 2024
1 parent ef748b6 commit 1681cde
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 5 deletions.
31 changes: 27 additions & 4 deletions resources/Js/Handlebars/join.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @copyright 2022-2024 Kévin Zarshenas
*/

const objectHash = require("object-hash");

/**
* Join
*
Expand All @@ -22,16 +24,37 @@
* {{join array '-'}}
* <!-- results in: 'a-b-c' -->
* ```
* @param {Array} `array`
* @param {Array, Object} `array`
* @param {String} `separator` The separator to use. Defaults to `, `.
* @return {String}
* @api public
*/
module.exports = (array, separator) => {

if (typeof array === 'string') return array;
if (!Array.isArray(array)) return '';
separator = typeof separator === "string" ? separator : ', ';
// Check is string
if (typeof array === 'string')

// Return string
return array;

// Check is object
if(typeof array == "object" && !Array.isArray(array))

// Push to array
array = Object.values(array);

// Check if array
if(!Array.isArray(array))

return '';

// Check separator
separator = typeof separator === "string"
? separator
: ', '
;

// Return result
return array.join(separator);

};
2 changes: 1 addition & 1 deletion src/Front/Library/Utility/Arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class Arrays {
* @param value The value to match for removal.
* @return A new array with the objects removed.
*/
public static removeObjectsByKeyValue = (array: Array<any>, key: string, value: string):Array<any> => {
public static removeObjectsByKeyValue = (array: Array<any>, key: string, value: string|number):Array<any> => {
return array.filter(obj => this.getNestedValue(obj, key.split('.')) !== value);
}

Expand Down
144 changes: 144 additions & 0 deletions src/Front/Library/Utility/Objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,149 @@ export default class Objects {
// Return result
return result;
}

/**
* Flatten
*
* Convert nested structure to a flat structure with separator
*
* @param obj - The object to flatten
* @param prefix - The prefix for keys (default is an empty string)
* @param separator - The separator used between keys (default is ".")
* @returns A flattened object
*/
public static flatten = (
obj: Record<string, any> = {},
prefix: string = '',
separator: string = '.'
): Record<string, any> => {

const result: Record<string, any> = {};

for (const [key, value] of Object.entries(obj)) {
const newKey = prefix ? `${prefix}${separator}${key}` : key;

if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
Object.assign(result, Objects.flatten(value, newKey, separator));
} else {
result[newKey] = value;
}
}

return result;

}

/**
* Unflatten
*
* Convert a flat object to a nested structure using a separator
*
* @param obj - The flat object to unflatten
* @param separator - The separator used in the keys (default is ".")
* @returns A nested object
*/
public static unflatten = (
obj: Record<string, any> = {},
separator: string = '.'
): Record<string, any> => {

const result: Record<string, any> = {};

for (const [key, value] of Object.entries(obj)) {
const parts = key.split(separator);
let temp = result;

for (let i = 0; i < parts.length; i++) {
const part = parts[i];

if (!temp[part] || typeof temp[part] !== 'object') {
temp[part] = {};
}

if (i === parts.length - 1) {
temp[part] = value;
} else {
temp = temp[part];
}
}
}

return result;

}

/**
* Deep merge
*
* Deeply merge multiple objects.
*
* @param createIfNotExists - Whether to create a new entry if the key does not exist in the merged object
* @param inputs - All objects to merge
* @returns The merged object
*/
public static deepMerge = (
createIfNotExists: boolean = false,
...inputs: Array<Record<string, any>>
): Record<string, any> => {

// Base object to start merging into
const merged: Record<string, any> = {};

// Iterate over each input object
for (const obj of inputs) {
for (const [key, value] of Object.entries(obj)) {
if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
// If the value is an object and the key exists in the merged object, merge them recursively
merged[key] = Objects.deepMerge(createIfNotExists, merged[key] || {}, value);
} else {
// Handle createIfNotExists flag
if (createIfNotExists && merged[key] !== undefined) {
merged[key] = Array.isArray(merged[key])
? [...merged[key], value]
: [merged[key], value];
} else {
// Otherwise, assign the value directly
merged[key] = value;
}
}
}
}

return merged;
}

/**
* Sort By Key
*
* @param input
* @param orderKey
* @param maxOrder
* @returns {Record<string, Item>}
*/
public static sortByKey = (
input: Record<string, Item>,
orderKey: string = 'order',
): Record<string, Item> => {
// Convert the object to an array of key-value pairs
const entries = Object.entries(input);

// Filter, handle missing `orderKey`, and sort based on the specified `orderKey` property
const sortedEntries = entries
.filter(([, value]) => typeof value[orderKey] === 'number' && value[orderKey] > 0)
.sort(([, a], [, b]) => a[orderKey] - b[orderKey]);

// Convert the sorted array back to an object
const sortedObject: Record<string, Item> = {};
for (const [key, value] of sortedEntries) {
sortedObject[key] = value;
}

return sortedObject;
}

}

interface Item {
[key: string]: any;
}

0 comments on commit 1681cde

Please sign in to comment.