GithubHelp home page GithubHelp logo

huynhhung0 / safe-any-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bilalreffas/safejson

0.0 1.0 0.0 193 KB

A safe wrapper for any and JSON written in TypeScript.

License: MIT License

TypeScript 98.80% JavaScript 1.20%

safe-any-1's Introduction

SafeAny

Build Status Coverage Status Dependencies Node Version

Safe wrapper for any and JSON in TypeScript.

Say goodbye to TypeError: Cannot read property 'key' of undefinied.

Install

npm i --save SamuelSchepp/safe-any

Quick start

import { SafeAny } from "safe-any";

// Unsafe object
const parsedJSON: any = {
  persons: [
    {
      firstName: "John",
      lastName: "Appleseed",
    },
  ],
};

// Create safe object
const object = new SafeAny(parsedJSON);

// Safe access
const value = object
  .get("persons")
  .get(0)
  .get("firstName")
  .stringOrDefault("no name");

// value === "John"

Guide

This libary makes access to objects of type any type-safe by providing default values and a typed interface.

Getting started

Create a SafeAny object by either wrapping an any or parsing a JSON literal. If the parsing fails, new SafeAny(null) will be returned. SafeAny.parseJSON() does not throw.

// Unsafe object, which may be the result of JSON.parse().
const unsafeObject: any = {
  ...
};
// Safe wrap
const safeObject = new SafeAny(unsafeObject);
// Parse JSON literal and wrap safely
const safeObject = SafeAny.parseJSON("{ 'key': 'value' }");

Referencing sub objects

get(key: string | number): SafeAny
const subValue1 = safeDictionary.get("key");
const subValue2 = safeArray.get(0);

Instead of accesing sub objects using the unsafe subscript access (object['key']), the get() operator is used. If the root object is an array, use get(2) to access the 3rd value. If the root object is a dictionary, use get("key") to access the value that is defined at the key key. If the sub object cannot be accessed, because it does not exist or the root object's type is incompatible, new SafeAny(null) is returned.

const object = {
    myArray: [
        {
            name: "object1",
        },
        {
            name: "object2",
        },
        {
            name: "object3",
        },
    ],
};

const safeObject = new SafeAny(object);
const nameOfObject2 = safeObject
  .get("myArray")
  .get(1)
  .get("name")
  .stringValue();

// nameOfObject2 === "object2"

Reading a value

  • Returns null if value is not present or the conversion failed.
stringOrNull(): string | null
numberOrNull(): number | null
booleanOrNull(): boolean | null
dictionaryOrNull(): { [key: string]: SafeAny } | null
arrayOrNull(): SafeAny[] | null

Reading a value with given fallback

  • Returns the given default value if value is not present or the conversion failed.
stringOrDefault(value: string): string
numberOrDefault(value: number): number
booleanOrDefault(value: boolean): boolean

Reading a value with conventional fallback

  • Either returns the value or a conventional default value.
stringValue(): string
numberValue(): number
booleanValue(): boolean
dictionaryValue(): { [key: string]: SafeAny }
arrayValue(): SafeAny[]
Function Fallback Value
stringValue() ""
numberValue() 0
booleanValue() false
dictionaryValue() {}
arrayValue() []

Getting the object's type

The type property can be used to determin the object's type. This is useful for custom parsing of tree structures or fallback mechanics.

Possible types are:

  • string
  • number
  • boolean
  • dictionary
  • array
  • null
import { SafeAny, Type } from "safe-any";

const wrappedDict = new SafeAny({ key: "value" });

if (wrappedDict.type === Type.dictionary) {
  const entry = wrappedDict.get("key");
  // entry.type === Type.string
} else {
  console.log("The value is no dictionary.");
}

Parsing embedded JSON string literals

If an object embeds a JSON string literal, use the parsed() operator to try parsing it into an object. If the object is a string, this operator treats the value as a JSON string and tries to parse it into an object and returns it.

If the object is no string or the parsing failed, the current object will be returned without change. This replaces the unhandy manual approach of breaking the operator chain.

const obj = {
  jsonString: "{ \"hello\": \"world\" }",
};
const safeObject = new SafeAny(obj);

// Instead of
SafeAny.parsedJSON(safeObject.get("jsonString").stringValue()).get("hello").stringValue();

// Do this
safeObject.get("jsonString").parsed().get("hello").stringValue();

safe-any-1's People

Contributors

samuelschepp avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.