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

@sqlpm/parse-json-ts

v1.0.4

Published

`pares-json-ts` is a typescript package that converts JSON text to a javascript object: optionally verifying and casting the object type.

Downloads

14

Readme

@sqlpm/parse-json-ts

@sqlpm/pares-json-ts is a typescript package that converts JSON text to a javascript object: optionally verifying and casting the object type.

README: See How to Use This Library to learn how to enable transpilation for local development or tools like jest or ts-node won't work.

Features

Usage

parseJson - Converting A String to a Javascript Object

Convert a string to a javascript object using the generic function parseJson. When options.verify is provided, parseJson can confirm that the object is of the type returned by the generic.

  • @param data - The string data which contains JSON.
  • @param [options]
    • [options.verify] - Verifies that the parsed object is of the desired type.
    • [options.revive] - Mutate the object during parsing.
  • @throws - Throws an error if the data is not valid JSON. If provided, verify might also throw an error.
  • @returns - A javascript object of the type defined by the generic.

@example Conversion without verification.

const json = '{"name": "Happy User", "age": 23}';

const user: unknown = parseJson(json);
expect(user).toEqual({
  name: 'Happy User',
  age: 23,
});

VerifySignature - Verifying Parsed Json Is Of A Given Type

Use a verify function to verify that the parsed JSON is of the expected type.

The verify function should return true when the JSON is of the expected type. The verify function should throw an Error otherwise.

@example Verify the object is a User.

const verifyUser: VerifySignature = (
  obj: object,
): true => {
  let verified = false;
  if (Array.isArray(obj)) {
    for (const item of obj) {
      if (!('name' in item && 'age' in item)) {
        break;
      }
    }
    verified = true;
  }

  if (verified) {
    return true;
  }
  throw Error('Unable to parse JSON to a User[] type.');
};

const json = '[{"name": "Happy User", "age": 23}]';
const user: User[] = parseJson<User[]>(json, { verify: verifyUser });

expect(user).toEqual([{
  name: 'Happy User',
  age: 23,
}]);

ReviverSignature - Altering Values During Parsing

Use a reviver function to transform values during parsing.

Each member of the parsed object leads to a call to the reviver function. For members with nested objects, the transformation of that nested object occurs before the member.

@remarks The reviver function can't be implemented using an arrow function.

  • @param this - The complete parsed object instance.
  • @param key - The active member in review.
  • @param value - The active member's value.
  • @returns - The final member's value.

@example A reviver function that adds 1 to age.

interface User {
  name: string;
  age: number;
}

const reviverUser: ReviverSignature = function reviver(
  this: unknown,
  key: string,
  value: any,
): any {
  let newValue = value;
  if (key === 'age') {
    newValue += 1;
  }
  return newValue;
};

const userJson = '[{"name": "Happy User", "age": 23}]';

// @remarks: Without providing a verify function, casting of the parsed
// json to User[] is done without verification and could lead to runtime
// errors.
const user: User[] = parseJson<User[]>(
  userJson,
  { reviver: reviverUser },
);

expect(user[0].age).toEqual(24);

Intent

  • No Emitted Javascript - The intent is to import this typescript library into a typescript project: compiling to Javascript occurring within the project.

Development

See the monorepo readme.

License

Licensed under MIT. See LICENSE.md.