Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
normalise-request
Advanced tools
Readme
Normalises API requests
$ 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.
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)
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'
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'
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'
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'
Mutates object
to normalise it according to the normaliser
.
Type: Object
The object to normalise.
Type: Object
The normaliser object that specifies the normalisations to apply to the object.
Trims the value to normalise if it is a string.
const normaliser = {
name: {
trim: true
}
};
Uppercases the value to normalise if it is a string.
const normaliser = {
name: {
toUpperCase: true
}
};
Lowercases the value to normalise if it is a string.
const normaliser = {
name: {
toLowerCase: true
}
};
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
}
};
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
}
};
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 regexnewSubStr
is the replacement stringconst normaliser = {
name: {
replace: { pattern: /H/g, newSubStr: 'Y'}
}
};
Converts the value to normalise to a float number, if that value is a string.
const normaliser = {
name: {
toFloat: true
}
};
Converts the value to normalise to an integer number, if that value is a string.
const normaliser = {
name: {
toInt: true
}
};
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'
}
};
Converts the value to normalise using the decodeAsUriComponent
global method,
if that value is a string.
const normaliser = {
name: {
decodeAsUriComponent: true
}
};
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: ','
}
};
MIT
This package was influenced by the package Validate.js.
FAQs
Normalises API requests
The npm package normalise-request receives a total of 16 weekly downloads. As such, normalise-request popularity was classified as not popular.
We found that normalise-request demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.