npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

shapify

v1.1.0

Published

Easily reshape objects

Downloads

24

Readme

shapify Build Status npm package coverage thanks

Easily reshape objects with TS support

Installation

npm install shapify
# or
yarn install shapify

Usage

Basic usage

Given an array of users:

const users = [
  { fullname: 'Eduardo San Martin morote', id: 1, more: 'properties', that: 'exist', but: "you don't always need"]},
  // more users
]

You can transform objects

import { shapify } from 'shapify'

const reshapedUser = shapify(
  {
    text: 'fullname',
    value: 'id',
  },
  users[0]
)
/**
 * only text and value are present
 * { text: 'Eduardo San Martin Morote', value: 1 }
 */

Functional approach

This can be used to transform whole arrays of objects:

import { shapify, shaper } from 'shapify'
// create a function with the first parameter fixed
const userShaper = shaper({ text: 'fullname', value: 'id' })
// equivalent to
const sameUserShaper = shapify.bind(null, { text: 'fullname', value: 'id' })

// generate the new array
const userChoices = users.map(userShaper)

⚠️: (#3) this breaks typings (mark them as unknown), if you know how to make it work, please file a PR

Customizing the new value

If you want to customize the value instead of just using the original one, you can provide a function:

const reshapedUser = shapify(
  {
    text: 'fullname',
    value: user => 'id: ' + user.id,
  },
  users[0]
)
/**
 * only text and value are present
 * { text: 'Eduardo San Martin Morote', value: 'id: 1' }
 */

Keeping original keys/values

If you need to keep original keys, you can provide a key with the same value:

shapify({ id: 'id' }, users[0])

But a more appropriate syntax is an array of keys (strings, numbers or symbols):

shapify(['id', 'fullname'], users[0])

This is similar to lodash.pick(users[0], ['id', 'fullname']). However, because shapify is type safe, it cannot support paths as strings as lodash.pick does. Instead you need to use a function.

If you need to keep all of the original keys, there is a helper that you can use:

import { shapify, keepKeys } from 'shapify'

// this will generate a user object with all of the original properties as well
// as text with fullname's value
shapify(
  {
    ...keepKeys(users[0]),
    text: 'fullname',
  },
  users[0]
)

License

MIT