flatten-deep-and-glue

1.0.0 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.0
    1
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.0
    1

Package Sidebar

Install

npm i flatten-deep-and-glue

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • dobromir-hristov