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

@kethan/uresolve

v0.0.1

Published

json resolver

Downloads

1

Readme

Resolve Function

Overview

The resolve function is a utility designed to process and resolve fields of an object or an array of objects. It allows you to define custom resolvers for specific fields and provides an option to convert objects before resolving them. This function is useful for transforming data structures in a flexible and modular way.

Installation

To use the resolve function in your project, simply import it from your module:

import { resolve } from 'uresolve';

Usage

Basic Usage

To use the resolve function, you need to define a set of resolvers for the fields you want to process. Each resolver is an asynchronous function that receives the field's value, the entire object being resolved, and the context. The resolve function returns a resolver object with a resolve method.

const resolvers = {
  name: async (value) => value.toUpperCase(),
  age: async (value) => value + 1
};

const resolver = resolve(resolvers);

const data = { name: 'Alice', age: 30 };

resolver.resolve(data, {}).then((resolved) => {
  console.log(resolved); // Output: { name: 'ALICE', age: 31 }
});

Using the Converter Option

You can provide a converter function in the options to transform objects before resolving them. The converter function receives the object and the context as arguments.

const converter = (data) => ({ ...data, converted: true });

const resolvers = {
  name: async (value) => value.toUpperCase()
};

const resolver = resolve(resolvers, { converter });

const data = { name: 'Alice' };

resolver.resolve(data, {}).then((resolved) => {
  console.log(resolved); // Output: { name: 'ALICE', converted: true }
});

Handling Nested Resolvers

The resolve function can handle nested objects by defining nested resolvers.

const resolvers = {
  name: async (value) => value.toUpperCase(),
  address: resolve({
    city: async (value) => value.toUpperCase()
  }).resolve
};

const data = { name: 'Bob', address: { city: 'san francisco' } };

resolver.resolve(data, {}).then((resolved) => {
  console.log(resolved); // Output: { name: 'BOB', address: { city: 'SAN FRANCISCO' } }
});

Array Conversion

The resolve function can process arrays of objects.

const converter = (data) => {
  if (Array.isArray(data.items)) {
    data.items = data.items.map((item) => ({ ...item, converted: true }));
  }
  return data;
};

const resolvers = {
  items: async (value) => value
};

const resolver = resolve(resolvers, { converter });

const data = {
  items: [
    { name: 'item1', price: 10 },
    { name: 'item2', price: 20 }
  ]
};

resolver.resolve(data, {}).then((resolved) => {
  console.log(resolved); 
  // Output: { items: [{ name: 'item1', price: 10, converted: true }, { name: 'item2', price: 20, converted: true }] }
});

Error Handling in Converter

If the converter function throws an error, it will be caught and handled appropriately.

const converter = (data) => {
  if (data.name === 'Dave') {
    throw new Error('Conversion error');
  }
  return data;
};

const resolvers = {
  name: async (value) => value.toUpperCase()
};

const resolver = resolve(resolvers, { converter });

resolver.resolve({ name: 'Dave' }, {}).catch((err) => {
  console.error(err.message); // Output: Conversion error
});

API

resolve(resolvers, options)

Parameters

  • resolvers (Object): An object where each key is a field name and each value is an asynchronous resolver function.
  • options (Object): Optional settings.
    • converter (Function): A function to convert objects before resolving. Receives the object and context as arguments.

Returns

An object with a resolve method.

resolve.resolve(obj, context)

Parameters

  • obj (Object|Array): The object or array of objects to resolve.
  • context (Object): An optional context object passed to resolver functions.

Returns

A Promise that resolves to the transformed object or array of objects.

Example

const converter = (data) => ({ ...data, converted: true });

const resolvers = {
  name: async (value) => value.toUpperCase(),
  age: async (value) => value + 1
};

const resolver = resolve(resolvers, { converter });

const data = { name: 'Alice', age: 30 };

resolver.resolve(data, {}).then((resolved) => {
  console.log(resolved); // Output: { name: 'ALICE', age: 31, converted: true }
});

License

This project is licensed under the MIT License.