Skip to content

Instantly share code, notes, and snippets.

@trev-dev
Created October 30, 2020 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trev-dev/815b9b8c849520663c915a605175b9d7 to your computer and use it in GitHub Desktop.
Save trev-dev/815b9b8c849520663c915a605175b9d7 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/gozebol
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
/**
* The following examplea is shopping cart that exists in some application state where the tree is
* productId: { variantId: quantity }. The goal is to get a list of all the variants
* in the cart using functional programming instead of imperative loops and mutable objects
*/
"use strict";
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var testCase = {
"3891858047040": {
"29250593947712": 1
},
"4353958182977": {
"31249973837889": 1
},
"1647551938624": {
"28302428766272": 1
},
"3929483182145": {
"29412702388289": 1
}
};
var combineArrays = function combineArrays(val, arr) {
return [].concat(_toConsumableArray(val), _toConsumableArray(arr));
};
var pipe = function pipe(start) {
return function () {
for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) {
funcs[_key] = arguments[_key];
}
return funcs.reduce(function (val, func) {
return func(val);
}, start);
};
};
var getSubKeys = function getSubKeys(levels) {
return function (object) {
var keys = Object.keys(object);
if (levels === 0) return keys.reduce(combineArrays);
var nextKeySet = getSubKeys(levels - 1);
return keys.map(function (k) {
return nextKeySet(object[k]);
});
};
};
var getOneLevelDeep = getSubKeys(1);
pipe(testCase)(getOneLevelDeep, console.log);
</script>
<script id="jsbin-source-javascript" type="text/javascript">/**
* The following examplea is shopping cart that exists in some application state where the tree is
* productId: { variantId: quantity }. The goal is to get a list of all the variants
* in the cart using functional programming instead of imperative loops and mutable objects
*/
const testCase = {
"3891858047040": {
"29250593947712": 1
},
"4353958182977": {
"31249973837889": 1
},
"1647551938624": {
"28302428766272": 1
},
"3929483182145": {
"29412702388289": 1
}
}
const combineArrays =
(val, arr) =>
[...val, ...arr]
const pipe =
start =>
(...funcs) =>
funcs.reduce((val, func) => func(val), start)
const getSubKeys =
levels =>
object => {
const keys = Object.keys(object)
if (levels === 0) return keys.reduce(combineArrays)
const nextKeySet = getSubKeys(levels - 1)
return keys.map(k => nextKeySet(object[k]))
}
const getOneLevelDeep = getSubKeys(1)
pipe(testCase)(getOneLevelDeep, console.log)</script></body>
</html>
/**
* The following examplea is shopping cart that exists in some application state where the tree is
* productId: { variantId: quantity }. The goal is to get a list of all the variants
* in the cart using functional programming instead of imperative loops and mutable objects
*/
"use strict";
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
var testCase = {
"3891858047040": {
"29250593947712": 1
},
"4353958182977": {
"31249973837889": 1
},
"1647551938624": {
"28302428766272": 1
},
"3929483182145": {
"29412702388289": 1
}
};
var combineArrays = function combineArrays(val, arr) {
return [].concat(_toConsumableArray(val), _toConsumableArray(arr));
};
var pipe = function pipe(start) {
return function () {
for (var _len = arguments.length, funcs = Array(_len), _key = 0; _key < _len; _key++) {
funcs[_key] = arguments[_key];
}
return funcs.reduce(function (val, func) {
return func(val);
}, start);
};
};
var getSubKeys = function getSubKeys(levels) {
return function (object) {
var keys = Object.keys(object);
if (levels === 0) return keys.reduce(combineArrays);
var nextKeySet = getSubKeys(levels - 1);
return keys.map(function (k) {
return nextKeySet(object[k]);
});
};
};
var getOneLevelDeep = getSubKeys(1);
pipe(testCase)(getOneLevelDeep, console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment