flatten-deep-and-glue
v1.0.0
Published
Flattens deeply nested arrays and objects and transforms them to <br> separated string by default.
Downloads
2
Readme
Introduction
flattenDeepAndGlue is a small function that expands objects or arrays of strings to separated strings, by default. The reason I made this module us to easily use Laravel's validation messages with sweetAlert2 or toastr. If you don't want to create a separated toast for each error, the plugin creates a string with rows for each error message value.
How to use
The examples use ES2015 syntax
With module loader
import flatten from 'flatten-deep-with-glue'
const
messageToTransform = { name: 'Name is required', email: 'Email is required' },
transformedMessage = flatten(messageToTransform) // 'Name is required <br/>Email is required'
In the Browser
<script src="path/to/flattenDeepAndGlue.js"></script>
<script>
var
messageToTransform = { name: 'Name is required', email: 'Email is required' },
transformedMessage = flattenDeepAndGlue(messageToTransform) // 'Name is required <br/>Email is required'
</script>
You could also pass deeply nested objects like [5, 'error', { name: 'Required' }]
and will get 5,error,Required
.
The second parameter is the separator so flatten(myMessage, ',')
would output a comma separated list of myMessage
's values.
Passing false to the separator param will just return a the flattened array without transforming it into a string