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

ngs-json-utils

v0.0.1

Published

A set of Angular utilities for deep cloning, serialization, and JSON manipulation.

Downloads

19

Readme

ngs-json-utils

ngs-json-utils is a lightweight utility library for Angular applications that provides easy-to-use functions for working with JSON objects. It includes methods for deep cloning, serialization, and deserialization of JSON data, designed specifically for Angular projects with TypeScript support.

Features

  • JSON Serialization & Parsing: Convert between objects and JSON with default values.
  • Deep Object Manipulation: Copy, merge, and update nested objects.
  • Object Comparison: Shallow and deep object equality checks.
  • Key Filtering: Filter objects by specified keys.
  • Map <-> JSON Conversion: Convert between JSON objects and Map.
  • Safe Key Search: Safe key lookup in objects.
  • Validation: Validate JSON strings for correctness.

Navigation


Installation & Usage in 3 steps

  1. Install ngs-json-utils via npm:
npm install ngs-json-utils
  1. Import NgsJsonUtilsService into your Angular component or service:
import { NgsJsonUtilsService } from "ngs-json-utils";
  1. Inject the service into your component by using dependency injection or the inject function:
export class MyComonent {
  constructor(private ngsJsonUtilsService: NgsJsonUtilsService) {}
  // or
  ngsJsonUtilsService = inject(NgsJsonUtilsService);
}

Methods

  • stringify

    Converts an object into a JSON string.
const result = this.ngsJsonUtilsService.stringify({ name: "John", age: 30 });

console.log(result); // {"name":"John","age":30}
  • parse

    Converts a JSON string into a JavaScript object.
const json = '{"name":"John","age":30}';
const obj = this.ngsJsonUtilsService.parse(json, { name: "Default", age: 0 });

console.log(obj); // { name: "John", age: 30 }

  • deepCopy

    Creates a deep copy of an object.
const original = { name: "John", details: { age: 30 } };
const copy = this.ngsJsonUtilsService.deepCopy(original);

console.log(copy); // { name: 'John', details: { age: 30 } }

  • isValidJSON

    Checks if a string is a valid JSON.
const isValid = this.ngsJsonUtilsService.isValidJSON('{"name":"John"}');

console.log(isValid); // true

  • equalJSON

    Compares two objects based on their serialized representations.
const obj1 = { name: "John" };
const obj2 = { name: "John" };
const areEqual = this.ngsJsonUtilsService.equalJSON(obj1, obj2);

console.log(areEqual); // true

  • deepEqualJSON

    Performs a deep comparison of two objects.
const obj1 = { name: "John", details: { age: 30 } };
const obj2 = { name: "John", details: { age: 30 } };
const areEqualDeep = this.ngsJsonUtilsService.deepEqualJSON(obj1, obj2);

console.log(areEqualDeep); // true

  • deepMerge

    Performs a deep merge of two objects.
const target = { name: "John", details: { age: 30 } };
const source = { details: { age: 31 }, address: "New York" };
const merged = this.ngsJsonUtilsService.deepMerge(target, source);

console.log(merged); // { name: 'John', details: { age: 31 }, address: 'New York' }

  • filterKeys

    Filters an object, keeping only specified keys.
const obj = { name: "John", age: 30, country: "USA" };
const filtered = this.ngsJsonUtilsService.filterKeys(obj, ["name", "country"]);

console.log(filtered); // { name: 'John', country: 'USA' }

  • deepUpdate

    Deeply updates an object based on another.
const target = { name: "John", details: { age: 30 } };
const updates = { details: { age: 31 }, country: "USA" };
const updated = this.ngsJsonUtilsService.deepUpdate(target, updates);

console.log(updated); // { name: 'John', details: { age: 31 }, country: 'USA' }

  • findValueByKey

    Finds a value by key in an object or nested object.
const obj = { name: "John", details: { age: 30 } };
const value = this.ngsJsonUtilsService.findValueByKey(obj, "age");

console.log(value); // 30

  • safeFindValueByKey

    Safely finds a value by key, returning undefined in case of an error.
const obj = { name: "John", details: { age: 30 } };
const value = this.ngsJsonUtilsService.safeFindValueByKey(obj, "age");

console.log(value); // 30

  • removeUndefined

    Removes all undefined values from an object.
const obj = { name: "John", age: undefined, country: "USA" };
const cleaned = this.ngsJsonUtilsService.removeUndefined(obj);

console.log(cleaned); // { name: 'John', country: 'USA' }

  • mapToJSON

    Converts a Map into a regular JSON object.
const map = new Map();
map.set("name", "John");
map.set("age", 30);
const json = this.ngsJsonUtilsService.mapToJSON(map);

console.log(json); // { name: 'John', age: 30 }

  • jsonToMap

    Converts a regular JSON object into a Map.
const json = { name: "John", age: 30 };
const map = this.ngsJsonUtilsService.jsonToMap(json);

console.log(map); // Map { 'name' => 'John', 'age' => 30 }

  • mergeUniqueObjects

    Merges multiple arrays of objects, keeping only unique objects based on a key.
const array1 = [
  { id: 1, name: "John" },
  { id: 2, name: "Jane" },
];
const array2 = [
  { id: 2, name: "Jane" },
  { id: 3, name: "Jim" },
];
const uniqueObjects = this.ngsJsonUtilsService.mergeUniqueObjects(array1, array2, "id");

console.log(uniqueObjects); // [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Jim' }]

Contributing

To contribute or use the library in development mode, you can clone the repository and install dependencies.

Steps to contribute:

  1. Fork the repository
  2. Clone the repo, install dependencies
git clone https://github.com/andrei-shpileuski/ngx-json-utils.git
cd ngx-json-utils
npm install
  1. Create a new branch for your changes
  2. Submit a pull request with a detailed description of the changes

Keywords

angular, json, utils, deep copy, cloning, serialization, deserialization, angular library, typescript, angular utils, ng, ngx, json utilities, json manipulation, deep merge, object comparison, angular helpers