Skip to content

Instantly share code, notes, and snippets.

@nolros
Last active March 1, 2024 06:16
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 nolros/722377abc6449effaba6c427dbb96743 to your computer and use it in GitHub Desktop.
Save nolros/722377abc6449effaba6c427dbb96743 to your computer and use it in GitHub Desktop.
function trampoline(fn) {
return function trampolined(...args) {
let result = fn(...args);
while (typeof result === 'function') {
result = result();
}
return result;
};
}
const setObjectPropsTrampoline = (accumulator, obj) => {
const go = (acc, keys) => {
if (keys.length === 0) {
return acc;
}
const [firstKey, ...restKeys] = keys;
const newAcc = { ...acc, [firstKey]: obj[firstKey] };
return () => go(newAcc, restKeys);
};
return trampoline(go)(accumulator, Object.keys(obj));
};
const result = setObjectPropsTrampoline({ path: 'home' }, { name: 'Nolan', last: 'Rosie' });
console.log('result:', result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment