Simple utils to pack arrays, objects and strings to a flat object (and back again).

Overview

packrup

NPM version NPM Downloads GitHub stars

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, objects 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)

Background

This package was built to support the utilities of zhead in an abstract way.

These utils were meant to be fully typed, but I struggled with the implementation. If you want a fun TypeScript challenge then feel free to open a PR :).

Installation

npm install --save-dev packrup

# Using yarn
yarn add --dev packrup

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([
  { 'http-equiv': 'content-security-policy', content: 'content-src none' }
])

// {
//    'content-security-policy': '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/[email protected]'
    },
    alt: 'Example Image'
  },
}, {
  key: 'image.src.1x',
  value: 'image.alt'
})

// {
//   "https://example.com/image.png": "Example Image",
// }

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",
// }

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 unpack to a string.

  • options

export interface TransformValueOptions {
  entrySeparator?: string
  keyValueSeparator?: string
  wrapValue?: string
  resolve?: (ctx: { key: string; value: unknown }) => string | void
}
import { unpackToString } from 'packrup'

unpackToString({
  'noindex': true,
  'nofollow': true,
  'max-snippet': 20,
  'maxi-image-preview': 'large',
}, {
  resolve({ key, value }) {
    if (typeof value === 'boolean')
      return `${key}`
  },
  keyValueSeparator: ':',
  entrySeparator: ', ',
})

// "noindex, nofollow, max-snippet:20, maxi-image-preview:large"

Sponsors

License

MIT License Β© 2022-PRESENT Harlan Wilton

You might also like...

Flat and simple color-picker library. No dependencies, no jquery.

Flat and simple color-picker library. No dependencies, no jquery.

Flat and simple color-picker Fully Featured demo Features Simple: The interface is straight forward and easy to use. Practical: Multiple color represe

Nov 14, 2022

🎨 Flat, simple, multi-themed, responsive and hackable Color-Picker library.

🎨 Flat, simple, multi-themed, responsive and hackable Color-Picker library.

🎨 Flat, simple, multi-themed, responsive and hackable Color-Picker library. No dependencies, no jQuery. Compatible with all CSS Frameworks e.g. Bootstrap, Materialize. Supports alpha channel, rgba, hsla, hsva and more!

Dec 27, 2022

Simple, Fast, Secure, Flat-File CMS

Bludit Simple, Fast and Flexible CMS. Bludit is a web application to build your own website or blog in seconds, it's completely free and open source.

Dec 30, 2022

Leader Board is a simple project based on JavaScript programing language. The purpose of this project is to work with APIs and ASYNC & AWAIT methods. I have used vanilla JavaScript with web pack to implement this project

Leader Board is a simple project based on JavaScript programing language. The purpose of this project is to work with APIs and ASYNC & AWAIT methods. I have used vanilla JavaScript with web pack to implement this project

Leader Board - JavaScript Project Table of contents Overview The challenge Screenshot Links Project Setup commands My process Built with What I learne

Oct 21, 2022

Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Dec 6, 2022

Sorting Arrays as simple as it gets.

Sort Sorting Arrays as simple as it gets. This module is published at: https://deno.land/x/sort. Simple Usage Example import { SortService, Direction

May 12, 2022

SUID is all a set of utils and components ported from MUI Core and much more.

Solid.js User Interface Design (SUID) A port of Material-UI (MUI) built with Solid.js SUID is all a set of utils and components ported from MUI Core a

Jan 1, 2023

Temporal-time-utils - This is a library with some reusable functions for Temporal.io TypeScript SDK

temporal-time-utils This is a library with some reusable functions for Temporal.io TypeScript SDK: sleepUntil: sleep to a specific date, instead of by

Oct 18, 2022

Dm-utils - Utility classes for ioBroker adapters to support ioBroker.dm

dm-utils Utility classes for ioBroker adapters to support ioBroker.dm. How to use In your ioBroker adapter, add a subclass of DeviceManagement and ove

Jan 2, 2022
Releases(v0.1.0)
Owner
Harlan Wilton
It's not knowing what to do; it's doing what you know.
Harlan Wilton
Basic website that allows users to add/remove books from a list. Achieved using JavaScript objects and arrays, dynamically modifying the DOM and adding basic events.

Awesome Books Basic website that allows users to add/remove books from a list. Achieved using JavaScript objects and arrays, dynamically modifying the

Didier Peran Ganthier 6 Dec 20, 2022
This is a basic app that allows user add/remove books from a list of books. It was created by using JavaScript arrays and objects to dynamically modify the DOM.

Awesome-Books This application lets you compile a dynamic collection of books using JavaScript. It is a single page app. You enter your book title & a

Jules Edozie 7 Oct 24, 2022
Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Alex MacArthur 196 Jan 6, 2023
Serialize an HTML Form to a JavaScript Object, supporting nested attributes and arrays.

jquery.serializeJSON Adds the method .serializeJSON() to jQuery to serializes a form into a JavaScript Object. Supports the same format for nested par

Mario Izquierdo 1.7k Dec 12, 2022
Construct ANSI colors strings from object descriptors.

ansi-construct Construct ANSI colors strings from object descriptors. Usage import { ansi } from 'ansi-construct' const item = ansi({ text: 'foo', co

Anthony Fu 22 Sep 8, 2022
Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Welcome to ts-object-keys ?? Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Funtal Foundation 1 Jul 4, 2022
A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fast, stores the Cache before stopping and restores it again!

remote-map-cache A remote nodejs Cache Server, for you to have your perfect MAP Cache Saved and useable remotely. Easy Server and Client Creations, fa

Tomato6966 8 Oct 31, 2022
Timers for Lost Ark bosses, islands, events, wandering merchants and more! Never miss an event again.

Timers for Lost Ark bosses, islands, events, wandering merchants and more! Never miss an event again. LostArkTimer.app Website Website Features Event

Joshua Kuan 28 Oct 17, 2022
Run a command, watch the filesystem, stop the process on file change and then run the command again...

hubmon Run a command, watch the filesystem, stop the process on file change and then run the command again... Install You can install this command lin

Hubert SABLONNIÈRE 7 Jul 30, 2022
A JavaScript library stores the form-data to the localstorage so you don't have to fill the form again.

form-storage A JavaScript library stores the form-data to the localstorage so you don't have to fill the form again. Installation via npm npm install

appleple 159 Dec 10, 2022