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

@sovgut/allocate

v1.0.11

Published

This npm package exports a handy TypeScript function named `allocate`. The package is designed to allow developers to replace keys in an object or an array of objects according to a provided key mapping schema. This is especially useful when you need to r

Downloads

41

Readme

allocate (@sovgut/allocate)

codecov

This npm package exports a handy TypeScript function named allocate. The package is designed to allow developers to replace keys in an object or an array of objects according to a provided key mapping schema. This is especially useful when you need to reshape data according to certain rules but you want to do it without spending significant time formatting your data.

Test Coverage

The allocate function is thoroughly tested to ensure its correctness and robustness. All of its edge cases are well covered and each new release is required to maintain or improve this high level of testing.

Installation

You can install allocate function npm package via npm:

npm install @sovgut/allocate

Examples

Consider the following examples on how you can use the allocate function:

Example 1 - Basic Usage

import {allocate} from '@sovgut/allocate';

const source = {_a: true};
const schema = {"_a": "a"};
const allocated = allocate(source, schema);
console.log(allocated); // Outputs: { a: true }

Example 2 - Nested Object Allocation

import {allocate} from '@sovgut/allocate';

const source = {_a: {_b: true}};
const schema = {"_a._b": "a.b"};
const allocated = allocate(source, schema);
console.log(allocated); // Outputs: { a: { b: true } }

Example 3 - Array Allocation

import {allocate} from '@sovgut/allocate';

const source = [{_a: true}, {_a: true}];
const schema = {"_a": "a"};
const allocated = allocate(source, schema);
console.log(allocated); // Outputs: [ { a: true }, { a: true } ]

Example 4 - Complex Allocation

import {allocate} from '@sovgut/allocate';

const source = {_a: {_b: [{_c: true}, {_c: true}]}};
const schema = {"_a._b.*._c": "a.b.*.c"};
const allocated = allocate(source, schema);
console.log(allocated); // Outputs: { a: { b: [ { c: true }, { c: true } ] } }

API Definition

type AllocateSchema = Record<string, string>;

export function allocate<TSource = any, TAllocated = any>(
    source: TSource,
    schema: AllocateSchema
): TAllocated;

Explanation:

  • On Function Parameters:
    • source: TSource - The structure you want to transform, this can be of any type (TSource), such as an object or an array.
    • schema: AllocateSchema - This is a map of key value pairs that define how to replace keys in the source.
  • On Return Type:
    • TAllocated - The function returns a new structure of type TAllocated. This transformed structure corresponds to the original source structure but its keys are modified as defined by the schema.
  • On Generic Types (TSource and TAllocated):
    • These types are placeholders for any type that will be replaced with the actual types when you call the function. The default type is any, but you can specify a more precise type according to your requirements.
  • On AllocateSchema type:
    • This is a type alias for the schema object which is a Record<string, string>, wherein both keys and values are string.

This function replaces the keys in a given source according to the provided schema and returns the newly created structure. Please note that this will not mutate the original source but create a new copy with the keys replaced. For example:

import {allocate} from '@sovgut/allocate';

const source = {_a: true};
const schema = {"_a": "a"};
const result = allocate(source, schema);
console.log(result); // Outputs: { a: true }

This example will replace the _a key from the source object with a as per the provided schema, and log the newly allocated object { a: true }.

If you have any suggestions or feedback, feel free to open an issue or send a PR.

Building and Bundling

1. Installing Dependencies

Before building the project, make sure to install all necessary dependencies. This can be done using npm. Navigate to your project root and run:

npm install

2. Bundling

This project uses esbuild for bundling JavaScript files into a single file in both CommonJS and ES Module format. This can be done using the bundle script specified in package.json. Run the bundle script with the following command:

npm run bundle

Upon success, you should now have the bundled files index.cjs and index.esm.js in your dist directory.

3. Testing

npm run test

Remember, before publishing the package, make sure all tests are passing.

License

MIT

Authors

Serhii Sovhut / Sergey Sovgut