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

normalise-request

v1.4.4

Published

Normalises API requests

Downloads

8

Readme

normalise-request

Normalises API requests

npm version Codeship Status for stevejay/normalise-request Coverage Status bitHound Overall Score bitHound Dependencies bitHound Dev Dependencies license

NPM

Install

$ npm install --save normalise-request

You can include this module using CommonJS format (require) or ES6 format (import):

const normalise = require('normalise-request');

// or

import normalise from 'normalise-request';

The examples in this README file are in CommonJS format.

Usage

Simple Objects

The most basic usage is normalising a simple object:

const normalise = require('normalise-request');

const request = {
    name: '   Steve   ',
    address: '  The Locksfords   '
};

const normaliser = {
    name: {
        trim: true
    },
    address: {
        trim: true
    }
};

normalise(request, normaliser);

// request.name is now 'Steve'
// request.address is now 'The Locksfords'

Normalisers are applied in declaration order:

// Example One:

const request = {
    name: '      ',
};

const normaliser = {
    name: {
        trim: true,
        undefinedIfEmpty: true
    }
};

normalise(request, normaliser);

// request.name is now undefined

// Example Two:

const request = {
    name: '      ',
};

const normaliser = {
    name: {
        undefinedIfEmpty: true,
        trim: true
    }
};

normalise(request, normaliser);

// request.name is now '' (the empty string)

Nested Objects

const normalise = require('normalise-request');

const request = {
    name: '   Steve   ',
    address: {
        firstLine: '  The Locksfords  ',
        secondLine: ' London '
    }
};

const normaliser = {
    name: {
        trim: true
    },
    address: {
        object: {
            firstLine: { trim: true },
            secondLine: { trim: true }
        }
    }
};

normalise(request, normaliser);

// request.name is now 'Steve'
// request.address.firstLine is now 'The Locksfords'
// request.address.secondLine is now 'London'

Arrays of Primitive Types

const normalise = require('normalise-request');

const request = {
    tags: ['  art  ', '   theatre   ']
};

const normaliser = {
    tags: {
        each: {
            trim: true
        }
    }
};

normalise(request, normaliser);

// request.tags[0] is now 'art'
// request.tags[1] is now 'theatre'

Arrays of Objects

const normalise = require('normalise-request');

const request = {
    tags: [
        { label: '  art  ' },
        { label: '   theatre   '}
    ]
};

const normaliser = {
    tags: {
        each: {
            object: {
                label: {
                    trim: true
                }
            }
        }
    }
};

normalise(request, normaliser);

// request.tags[0].label is now 'art'
// request.tags[1].label is now 'theatre'

Adding a new normaliser

const normalise = require('normalise-request');

const changeToFoo = (param, options) => {
    // param - the value this normaliser is being applied to.
    // options - the object assigned to the normaliser 
    return 'foo';
};

normalise.normalisers.changeToFoo = changeToFoo;

// use it like a built-in normaliser:

const normaliser = {
    name: {
        changeToFoo: { someOption: 'value' }
    }
}

// name will become 'foo'

API

normalise(object, normaliser)

Mutates object to normalise it according to the normaliser.

object

Type: Object

The object to normalise.

normaliser

Type: Object

The normaliser object that specifies the normalisations to apply to the object.

Normalisers

trim

Trims the value to normalise if it is a string.

const normaliser = {
    name: {
        trim: true
    }
};

toUpperCase

Uppercases the value to normalise if it is a string.

const normaliser = {
    name: {
        toUpperCase: true
    }
};

toLowerCase

Lowercases the value to normalise if it is a string.

const normaliser = {
    name: {
        toLowerCase: true
    }
};

undefinedIfEmpty

Sets the value to normalise to undefined if it is a zero-length string or a zero-length array, or if the value is null.

const normaliser = {
    name: {
        undefinedIfEmpty: true
    }
};

collapseWhitespace

Collapses whitespace in the value to normalise if it is a string. Any runs of multiple whitespace characters are each replaced by a single space character. If using this normaliser on a value, you would normally also use the trim normaliser.

const normaliser = {
    name: {
        collapseWhitespace: true
    }
};

replace

Replaces matching strings in the value to normalise, if that value is a string. This normaliser uses the String.replace method to do the replacement, so the arguments to this normaliser and its behaviour correspond to those of that string method:

  • pattern can be a string or a regex
  • newSubStr is the replacement string
const normaliser = {
    name: {
        replace: { pattern: /H/g, newSubStr: 'Y'}
    }
};

toFloat

Converts the value to normalise to a float number, if that value is a string.

const normaliser = {
    name: {
        toFloat: true
    }
};

toInt

Converts the value to normalise to an integer number, if that value is a string.

const normaliser = {
    name: {
        toInt: true
    }
};

default

Sets the value to normalise to a replacement value, if that value to normalise is null or undefined.

const normaliser = {
    name: {
        default: { value: 'the default value' }
    }
};

// or

const normaliser = {
    name: {
        default: 'the default value'
    }
};

decodeAsUriComponent

Converts the value to normalise using the decodeAsUriComponent global method, if that value is a string.

const normaliser = {
    name: {
        decodeAsUriComponent: true
    }
};

split

Converts the value to normalise using the string.split method, if that value is a string. The argument to the normaliser is the separator string for the split.

const normaliser = {
    name: {
        split: { separator: ',' }
    }
};

// or 

const normaliser = {
    name: {
        split: ','
    }
};

License

MIT

Acknowledgements

This package was influenced by the package Validate.js.