Package Exports
- packrup
Readme
packrup
Simple utils to pack (and unpack) arrays and strings to a flat object.
Status: In Development Please report any issues 🐛 Made possible by my Sponsor Program 💖 Follow me @harlan_zw 🐦 • Join Discord for help |
Features
- Pack arrays and strings to a flat object
- Handles duplicates with
key
- Supports nested key selections with
dot.notation
- 🌳 Composable, tree-shakable and tiny (< 1kb, see export-size-report)
Installation
npm add -D packrup
Pack API
packArray
Arguments
input -
array
The array to pack
options -
{ key: string | string[], value: string | string[] }
The options to use to resolve the key and value. By default, will choose first 2 keys of an object.
import { packArray } from 'packrup'
packArray([
{ httpEquiv: 'content-security-policy', content: 'content-src none' }
])
// {
// httpEquiv: 'content-src none',
// }
packObject
Arguments
input -
object
The record to pack.
options -
{ key: string | string[], value: string | string[] }
The options to use to resolve the key and value. By default, will choose first 2 keys of an object.
import { packObject } from 'packrup'
packObject({
image: {
src: {
'1x': 'https://example.com/image.png',
'2x': 'https://example.com/image@2x.png'
},
alt: 'Example Image'
},
}, {
key: 'image.src.1x',
value: 'image.alt'
})
// {
// httpEquiv: 'content-src none',
// }
packString
import { packString } from 'packrup'
const head = packString('src="https://example.com/image.jpg" width="800" height="600"')
// {
// "height": "600",
// "src": "https://example.com/image.jpg",
// "width": "800",
// }
Unpack API
unpackToArray
Arguments
input -
array
The array to pack
options -
{ key: string | string[], value: string | string[] }
The options to use to resolve the key and value. By default, will choose first 2 keys of an object.
import { unpackToArray } from 'packrup'
unpackToArray({
'content-security-policy': 'content-src none',
},
{ key: 'http-equiv', value: 'content' }
)
unpackToString
Arguments
input -
object
The record to pack.
options -
{ key: string | string[], value: string | string[] }
The options to use to resolve the key and value. By default, will choose first 2 keys of an object.
import { unpackToString } from 'packrup'
packObject({
image: {
src: {
'1x': 'https://example.com/image.png',
'2x': 'https://example.com/image@2x.png'
},
alt: 'Example Image'
},
}, {
key: 'image.src.1x',
value: 'image.alt'
})
// {
// httpEquiv: 'content-src none',
// }
packString
import { packString } from 'packrup'
const head = packString('src="https://example.com/image.jpg" width="800" height="600"')
// {
// "height": "600",
// "src": "https://example.com/image.jpg",
// "width": "800",
// }
Sponsors
License
MIT License © 2022-PRESENT Harlan Wilton