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

object-case-builder

v1.0.0

Published

Object case builder

Downloads

2

Readme

Description

Is simple util who can change object key names by case strategy. For example, your yml config uses snake case but you need a javascript object with keys in the lower camel case. It is this simple functionality give this package!

Installation

$ npm i --save object-case-builder

Examples

snake-case to lower camel case

import {
  ObjectCaseBuilder,
  PropertyNamingStrategyEnum,
} from 'object-case-builder';

const input: object = {
  'max_users_count': 1,
  'auth_secret_token': 'SECRET TOKEN',
  'credit_card_number': 00000000000000,
}

const output: object = new ObjectCaseBuilder()
  .setPropertyNamingStrategy(PropertyNamingStrategyEnum.LOWER_CAMEL_CASE)
  .formatObject(
    input,
    PropertyNamingStrategyEnum.SNAKE_CASE
  );

console.log(output);

/* console output
{
  maxUsersCount: 1,
  authSecretToken: 'SECRET TOKEN',
  creditCardNumber: 00000000000000
}
*/

kebab-case to screaming-snake-case

import {
  ObjectCaseBuilder,
  PropertyNamingStrategyEnum,
} from 'object-case-builder';

const input: object = {
  'message-content': 'test',
  'timestamp': 1673698827991,
  'author-name': 'nikyoff',
}

const output: object = new ObjectCaseBuilder()
  .setPropertyNamingStrategy(PropertyNamingStrategyEnum.SCREAMING_SNAKE_CASE)
  .formatObject(
    input,
    PropertyNamingStrategyEnum.KEBAB_CASE
  );

console.log(output);

/* console output
{
  'MESSAGE_CONTENT': 'test',
  'TIMESTAMP': 1673698827991,
  'AUTHOR_NAME': 'nikyoff',
}
*/

Build class output

Sometime you need transform your output to class and thats possible!

//message-data.ts
import { Expose, Transform } from "class-transformer";

export class MessageData {
  @Expose()
  @Transform(params => params.value instanceof Date ? params.value : new Date(params.value))
  public createTimestamp: Date;

  @Expose()
  public messageContent: string;
}
import {
  ObjectCaseBuilder,
  PropertyNamingStrategyEnum,
} from 'object-case-builder';
import { MessageData } from './message-data'


const input: object = {
  'create_timestamp': 1673698827991,
  'message-content': 'test',
}

const output: MessageData = new ObjectCaseBuilder<MessageData>()
  .setPropertyNamingStrategy(PropertyNamingStrategyEnum.LOWER_CAMEL_CASE)
  .formatObject(
    input,
    PropertyNamingStrategyEnum.SNAKE_CASE,
    MessageData
  );

Register custom case

Implement class for your case

//screaming-snake.property-naming-strategy.ts
export class ScreamingSnakePropertyNamingStrategy extends PropertyNamingStrategy {
  public propertyNameToWords(property: string): string[] {
    return property.split('_');
  }

  public wordsToPropertyName(words: string[]): string {
    return words.join('_').toUpperCase();
  }
}

Register strategy

import { PropertyNamingRegister } from 'object-case-builder';
import { ScreamingSnakePropertyNamingStrategy } from './screaming-snake.property-naming-strategy';

PropertyNamingRegister.registerStrategy(
  'SCREAMING_SNAKE_CASE',
  ScreamingSnakePropertyNamingStrategy,
);