Giter Site home page Giter Site logo

alternating-case-to-object's Introduction

Alternating Case to Object

A function to convert ALTERNATINGcase strings to objects.

Usage

npm i @abcnews/alternating-case-to-object
const alternatingCaseToObject = require('@abcnews/alternating-case-to-object');

alternatingCaseToObject('PROPvalueSECONDthingALLOWEDyesTHINGS100');

// >>>

{
  prop: 'value',
  second: 'thing',
  allowed: true,
  things: 100
}

Automatic type inference

  • Number values will be parsed as floats.
  • The values "true", "yes", "false" & "no" will be converted to the booleans true, true, false & false, respectively.
  • If a prop appears more than once, multiple values will be returned as an array:
alternatingCaseToObject('GROUPfirstGROUPsecondGROUPthird');

// >>>

{
  group: ['first', 'second', 'third'];
}

Every value in a prop which appears multiple times must be of the same type or an exception will be thrown. For example, this will throw:

alternatingCaseToObject('AtrueAstr');

Options

You can pass an object as a second argument, defining one or more options:

arrayProps:Array

Props in this array will always be returned as arrays, even if they occur in the config string zero or one time.

alternatingCaseToObject('AtrueAfalseBvalueAtrueDvalue', {
  arrayProps: ['a', 'b', 'c']
});

// >>>

{
  a: [true, false, true],
  b: ['value'],
  c: [],
  d: 'value'
}

propMap:Object

Props in the config will be renamed if they occur in this object.

alternatingCaseToObject('CAMELCASEtrueKEBABCASE100', {
  propMap: {
    camelcase: 'camelCase',
    kebabcase: 'kebab-case'
  }
});

// >>>

{
  camelCase: true,
  'kebab-case': 100
}

alternating-case-to-object's People

Contributors

colingourlay avatar dependabot[bot] avatar drzax avatar nathanhoad avatar phocks avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

alternating-case-to-object's Issues

[Security] Workflow size.yml is using vulnerable action actions/checkout

The workflow size.yml is referencing action actions/checkout using references v1. However this reference is missing the commit a6747255bd19d7a757dbdda8c654a9f84db19839 which may contain fix to the some vulnerability.
The vulnerability fix that is missing by actions version could be related to:
(1) CVE fix
(2) upgrade of vulnerable dependency
(3) fix to secret leak and others.
Please consider to update the reference to the action.

Add a helper function to assert shape of returned objects

This library takes arbitrary string input and returns a typed object. However the types are, necessarily, extremely generic because we expect a wide range of possible data.

So while the returned types are useful, the reality is that most apps using the library will need to conduct their own runtime type assertions to guarantee data matches the types the app is expecting.

It would be useful to simplify that process and provide a generic function that could provide more strongly typed results based on a passed type structure. A structure like the below should allow the return of a more strongly typed result.

type Shape = {
  [key: string]: {
    primitive: "string" | "number" | "boolean" | "null",
    isArray: boolean,
    isRequired: boolean
  }
}

Implement objectToAlternatingCase function

In response to Ash's question, it might be an idea to quickly implement this functionality.

So we can do somthing like:

import { objectToAlternatingCase } from "@abcnews/alternating-case-to-object";

const myObject = { test: "object" };

const myString: string = objectToAlternatingCase(myObject);

@AshKyd has said he's rolled his own version of this, so it could be a simple matter of pasting it in this repo with a little export.

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.