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

csh-file-formatter-module

v1.2.2

Published

Module to format JSON with common transformations

Downloads

32

Readme

csh-file-formatter-module

Wrapper around json-map-transformer to map JSON objects using common transformations.

Installation

npm i csh-file-formatter-module

Usage

const { JSONTransformer } = require('csh-file-formatter-module');

const formater = new JSONTransformer(template, staticKeys, configOptions);

console.log(formater.transform(obj, afterTransform))

Template

key: Property name on the output object

path: Property in the input object that will be mapped. Can also be an array of possible paths. Can be omitted if the key will be the same.

transform: Function that receives input value/Name of a predefined transformation function. Can be an array of mixed values.

default: Value to set if the transformed value is undefined. Can also be a function that will be executed when the JSONTransformer is created.

After transformation, undefined and empty strings are omitted.

Example:

{
    timestamp: {
        path: 'updatedAt',
        transform: 'LOCALE_STRING'
    },
    name: 'author', // same as { path: 'author', omit: ['', undefined] },
    id: {
        transform: (id) => id + '123'
    },
    description: {
        transform: ['NORMALIZE', (description) => description.replace('a', 'e')] // normalize(description).replace(...)
    }
}

Transformations

The following transformations are predefined:

| name | description | | ------------- | ----------- | | Strings | | NORMALIZE_ALL | Replace : with _ and spaces with - | | NORMALIZE | Replace spaces with - | | UPPERCASE | Converts string to uppercase | | LOWERCASE | Converts string to lowercase∫ | | MAX_CHARS | Returns substring(0, maxChars) | | TAGS_NORM | Removes tags from string | | SPACES_NORM | Removes \\n and \\r characters with single space. Also removes multiple spaces with single one | | HTML_NORM | Replaces HTML entities to proper char, removes unicode and special chars | | URLs | | URL_ORIGIN | Returns origin from an url | | URL_PATH | Returns path from an url | | Dates | | LOCALE_STRING | Transform string date to locale string date (within timezone specified) | | YEAR_MONTH | Gets YYYYMM format from a ISO string date | | ISO_STRING | Returns ISO string from a date | | SPLIT_TIMESTAMP | Given a ISO date or UNIX timestamp, returns an array with date components |

Static Keys

Object with values to add to the output object. Each key must be a value or function. In case of functions, they are evaluated each time transform is executed.

Example:

{
    origin: 'a value',
    number: () => 5 
}

Config options

There are possible variables to configure:

  • maxChars. Sets the value for MAX_CHARS transformation. Defaults to 260000.
  • timezone. Time zone used for LOCALE_STRING transformation.
  • keepUnused. Boolean to indicate the unused fields from original object should be kept. The fields unused are stored on unusedFieldsKey key and are transformed with JSON.stringify (see stringifyUnused). Defaults to false.
  • unusedFieldsKey. Name of the key where the unused fields are stored. Defaults to meta. If the value passed is . the unused keys will be added in the output object.
  • stringifyUnused. Indicates if the unused fields should be stringified or not. Defaults to true.
  • afterTransform. Function that will receive the output object and process it. Can be overrided on the transform method. Defaults to: x => x.

.transform(obj, afterTransform)

Params

obj: Object to be transformed

afterTransform: Function to be used on the transformation. Defaults to configOptions.afterTransform

Example:

myTransformer.transform(obj, (outputObj, inputObject) => {
    outputObj.processed = 1 + inputObject.count;
    return outputObj;
})

Example

const { JSONTransformer } = require('csh-file-formatter-module');

const template = {
    slug: {
        transform: ['NORMALIZE', 'UPPERCASE']
    },
    description: 'details',
    url: {
        path: 'source.url',
        transform: 'URL_ORIGIN'
    },
    author: {
        transform: author => 'By ' + author
    },
    date: {
        path: 'created_at',
        transform: 'LOCALE_STRING'
    }
};

const staticKeys = {
    source: 'json-map-transform',
    id: () => {
        return 'addd';
    }
}

const configOpts = {
    keepUnused: true,
    timezone: 'America/Bogota'
}

function afterTransform(outputObj) {
    outputObj.identifier = `${outputObj.slug}-${outputObj.id}`;
    return outputObj;
}

const formater = new JSONTransformer(template, staticKeys, configOpts);
const output = formater.transform({
    slug: 'a new challenge',
    details: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempor.',
    source: {
        url: 'https://es.lipsum.com/feed/html',
        provider: 'lipsum'
    },
    author: 'sit amet',
    location: 'Spain',
    created_at: '2019-05-03T11:15:43Z'
}, afterTransform);

// output = 
// {
//   slug: 'A-NEW-CHALLENGE',
//   description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tempor.',
//   url: 'https://es.lipsum.com',
//   author: 'By sit amet',
//   date: '05/03/2019, 06:15:43',
//   meta: '{"source":{"url":"https://es.lipsum.com/feed/html","provider":"lipsum"},"location":"Spain"}',
//   source: 'json-map-transform',
//   id: 'cc612cda-1535-4221-b7f8-f86259689562'
//   identifier: 'A-NEW-CHALLENGE-cc612cda-1535-4221-b7f8-f86259689562'
// }