Skip to content

Instantly share code, notes, and snippets.

View kraftdorian's full-sized avatar

Damian kraftdorian

View GitHub Profile
@kraftdorian
kraftdorian / flattenObject.js
Last active March 6, 2021 18:36
Flatten object with paths
const flattenObject = (value, key = null, flattenedObject = {}) => {
if (
['string', 'number', 'bigint', 'boolean', 'undefined', 'symbol'].includes(typeof value) ||
value === null ||
value instanceof Date
) {
flattenedObject[key] = value;
} else if (Array.isArray(value)) {
value.forEach((childValue, childIndex) => {
flattenObject(childValue, `${key}[${childIndex}]`, flattenedObject);
@kraftdorian
kraftdorian / transformObjectToIdealTree.js
Last active March 6, 2021 18:44
Convert nested object to ideal tree
const transformObjectToIdealTree = (node, { nodeIdPropName = 'name', nodeChildrenPropName = 'children' }) => {
const transform = (node) => {
const childNodes = Object.keys(node).map(child => ({ [nodeIdPropName]: child }));
for (const childNode of childNodes) {
childNode[nodeChildrenPropName] = transform(node[childNode[nodeIdPropName]]);
}
return childNodes;
};
return transform(node);
};
@kraftdorian
kraftdorian / unicodeSquareGrid.js
Last active March 21, 2021 11:20
Draw unicode square grid
const GRID_PARTS_VARIANT_LIGHT = 'LIGHT';
const GRID_PARTS_VARIANT_DOUBLE = 'DOUBLE';
const GRID_PART_VARIANTS_DICTIONARY = {
[GRID_PARTS_VARIANT_LIGHT]: '─│┌┐└┘├┤┬┴┼',
[GRID_PARTS_VARIANT_DOUBLE]: '═║╔╗╚╝╠╣╦╩╬'
};
/**
* Create grid parts mapping object
@kraftdorian
kraftdorian / usePrologStyleList.js
Created April 3, 2021 19:08
Prolog style list in JavaScript
const usePrologStyleList = (array) => {
const [head, ...tail] = array;
return [head, tail];
};
@kraftdorian
kraftdorian / recursivePrologStyleInsertSort.js
Last active April 4, 2021 10:36
Recursive insert sort in JavaScript written in Prolog style
const allowedSortValueTypes = ['string', 'number', 'bigint'];
const usePrologStyleList = (array) => {
const [head, ...tail] = array;
return [head, tail];
};
const insertState = (value, list, acc, isInserted) => {
const [head, tail] = usePrologStyleList(list);
@kraftdorian
kraftdorian / recursivePrologStyleListMinimum.js
Last active April 17, 2021 11:14
Recursive list minimum in JavaScript written in Prolog style
const usePrologStyleList = (array) => {
const [head, ...tail] = array;
return [head, tail];
};
const listMinimumState = (list, minimum, acc) => {
const [head, tail] = usePrologStyleList(list);
if (head === undefined) {
return minimum = acc;
@kraftdorian
kraftdorian / recursivePrologStyleListReverse.js
Created April 5, 2021 18:53
Recursive list reverse in JavaScript written in Prolog style
const usePrologStyleList = (array) => {
const [head, ...tail] = array;
return [head, tail];
};
const listReverseState = (list, reversedList, acc) => {
const [head, tail] = usePrologStyleList(list);
if (head === undefined) {
return reversedList = acc;
@kraftdorian
kraftdorian / recursiveErlangStyleStringSplit.js
Created April 15, 2021 19:20
Recursive string split in JavaScript written in Erlang style
const usePrologStyleList = (array) => {
const [head, ...tail] = array;
return [head, tail];
};
const useErlangStyleString = (string, resultString) => {
const length = string.length;
if (!length) {
return resultString;
}
@kraftdorian
kraftdorian / api.mjs
Last active April 24, 2021 13:41
Case match pattern in JavaScript written in Erlang style
import {case_spec, match_spec} from './lib.mjs';
const _case = (expression) => {
return {
of: (...predicates) => {
return case_spec(expression, () => predicates.reduce((partial, expr) => {
return {
...partial,
[match_spec(expression, expr.matchSpec)]: expr.matchFn
};
@kraftdorian
kraftdorian / scalaStyleTuple.js
Last active April 24, 2021 10:42
Tuple in JavaScript written in Scala style
const tuple = (...atoms) => Object.freeze(
atoms.reduce((partial, atom, index) => ({
...partial,
['_' + ++index]: atom
}), {})
);