-
-
Save s-m-i-t-a/012959d52b2373cedd9af1f55e3f6deb to your computer and use it in GitHub Desktop.
Transducer to transform and convert native JS iterable data types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MAIN iterator | |
function isInstanceOf(output, input, instanceOfType) { | |
return output ? output instanceof instanceOfType : input instanceof instanceOfType | |
} | |
function asSet(data) { | |
return new Set(data.values()) | |
} | |
function asArray(data) { | |
return Array.from(data.values()); | |
} | |
function asIterable(func, convertor) { | |
return function(...args) { | |
response = func(...args) | |
return convertor(response); | |
} | |
} | |
function reduce(input, transformator) { | |
let response = new Map(); | |
for (kv of input.entries()) { | |
response = transformator((result, [key, value]) => { | |
return result.set(key, value); | |
})(response, kv) | |
} | |
return response; | |
} | |
const reduceToSet = asIterable(reduce, asSet); | |
const reduceToArray = asIterable(reduce, asArray); | |
// REDUCERS | |
const mapReducer = (f) => (reducing) => (result, input) => reducing(result, f(input)); | |
const filterReducer = (predicate) => (reducing) => (result, input) => predicate(input) ? reducing(result, input) : result; | |
// TRANSFORMATORS | |
// Mappers | |
const stringifyMapper = mapReducer(([key, value]) => { | |
return [key, `${value}`]; | |
}) | |
const addMapper = (count) => mapReducer(([key, value]) => { | |
return [key, value + count]; | |
}); | |
const convertToJSXComponentExample = mapReducer(([key, value]) => { | |
return [ | |
key, | |
`<div id=${key}>${value}</div>` | |
] | |
}) | |
// Filters | |
const evenFilter = filterReducer(([key, value]) => (value % 2 === 0)); | |
// Transformators are composable | |
const addStringifyFilterTransformator = R.compose( | |
addMapper(1), | |
stringifyMapper, | |
evenFilter | |
); | |
const evenToJSXTransformator = R.compose( | |
addMapper(1), | |
evenFilter, | |
convertToJSXComponentExample | |
) | |
// PLAYGROUND | |
const arr = [ 1, 2, 3 ]; | |
const map = new Map([ ['id1', 1], ['id2', 2], ['id3', 3] ]); | |
const set = new Set([1, 2, 3]); | |
console.group('United data transformation across native JS iterable data types') | |
// convert to set | |
asSet(reduce( arr, addStringifyFilterTransformator)) | |
reduceToSet( arr, addStringifyFilterTransformator) | |
// convert to Array | |
asArray(reduce( arr, addStringifyFilterTransformator)) | |
reduceToArray( arr, addStringifyFilterTransformator) | |
// plain data | |
reduce( arr, addStringifyFilterTransformator) | |
console.log( | |
reduce( map, addStringifyFilterTransformator, [] ) | |
); | |
console.log( | |
reduce( set, addStringifyFilterTransformator, [] ) | |
); | |
console.log( | |
reduce( arr, addStringifyFilterTransformator, new Map() ) | |
); | |
console.log( | |
reduce( map, addStringifyFilterTransformator, new Map() ) | |
); | |
console.log( | |
reduce( set, addStringifyFilterTransformator, new Map() ) | |
); | |
console.log( | |
reduce( arr, addStringifyFilterTransformator, new Set() ) | |
); | |
console.log( | |
reduce( map, addStringifyFilterTransformator, new Set() ) | |
); | |
console.log( | |
reduce( set, addStringifyFilterTransformator, new Set() ) | |
); | |
console.groupEnd() | |
console.group('Output argument is optional') | |
console.log( | |
reduce(arr, addStringifyFilterTransformator) | |
); | |
console.log( | |
reduce(map, addStringifyFilterTransformator) | |
); | |
console.log( | |
reduce(set, addStringifyFilterTransformator) | |
); | |
console.groupEnd() | |
console.group('Filter and convert') | |
console.log( | |
reduce(arr, evenToJSXTransformator) | |
); | |
console.log( | |
reduce(map, evenToJSXTransformator) | |
); | |
console.log( | |
reduce(set, evenToJSXTransformator) | |
); | |
console.groupEnd() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment