JSPM

ngs-json-utils

0.0.1
    • ESM via JSPM
    • ES Module Entrypoint
    • Export Map
    • Keywords
    • License
    • Repository URL
    • TypeScript Types
    • README
    • Created
    • Published
    • Downloads 5
    • Score
      100M100P100Q27018F
    • License MIT

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

    Package Exports

    • ngs-json-utils
    • ngs-json-utils/package.json

    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.

    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