1.0.4 • Published 4 years ago
switchr v1.0.4
Switchr
Switchr provides a simple, clean, and flexible alternative to switch statements.
Installation
npm install switchr
Usage
Methods
Basic Usage
const basicSwitchr = switchr({
foo: 'foo foo',
buzz: (str) => {
return 'buzz buzz ' + str;
},
default: 'foobarbuzz'
});
basicSwitchr.get('foo'); // 'foo foo'
basicSwitchr.get('buzz', 'baby'); // 'buzz buzz baby'
basicSwitchr.get('non-existent'); // 'foobarbuzz'
Initialization
- Import
import switchr from 'switchr'; /* ES6 */
/* OR */
const switchr = require('switchr').default; /* require */
- Switchr can be initialized with either an object or array for ultimate flexibility:
// Object
const switchrObject = switchr({
foo: 'foo',
bar: 'bar'
});
// Array
const switchrArray = switchr([
{
key: 'foo',
value: 'foo'
},
{
key: 'bar',
value: 'bar'
}
]);
- Initializing with an array will allow common functionality between multiple cases using the keys property:
const switchrArray = switchr([
{
keys: ['one', 'two', 'three'],
value: 1
},
{
key: 'four',
value: 4
}
]);
switchrArray.get('one'); // 1
switchrArray.get('two'); // 1
switchrArray.get('three'); // 1
switchrArray.get('four'); // 4
- It also accepts functions as values
const switchrWithFunction = switchr({
foo: (a, b) => {
return a + b;
},
});
const switchrWithArrowFunction = switchr({
bar: (a, b) => {
return a - b;
}
});
Get
- The
get
method retrieves simple values:
const getSwitchr = switchr({
getMe: 'You got me!'
});
getSwitchr.get('getMe'); // 'You got me!'
- Or executes functions
const getFunctionsSwitchr = switchr({
getMeFn: () => { // Without arguments
return 'You got me again!';
},
sum: (a, b) => { // With arguments
return a + b;
}
});
getSwitchr.get('getMeFn'); // 'You got me again!'
getSwitchr.get('sum', 10, 15); // 25
get
will ignore arguments passed in when the function doesn't expect them:
getSwitchr.get('getMeFn', 10, 15) // 'You got me again!'
- Use a default key to handle cases that don't exist
const switchrWithDefault = switchr({
foo: 'foo',
default: 'foobar'
});
switchrWithDefault.get('doesntExist') // 'foobar'
- If no default key exists,
get
will return null:
const switchrWithoutDefault = switchr({
foo: 'foo'
});
switchrWithDefault.get('doesntExist') // null
Set
- The
set
method adds a new case by passing in a key and value:
const setSwitchr = switchr({
foo: 'foo'
});
setSwitchr.set('bar', 'bar');
setSwitchr.get('bar'); // 'bar'
set
also accepts a callback function:
function multiply(a, b) {
return a * b;
}
const setSwitchr = switchr({
foo: 'foo'
});
setSwitchr.set('multiply', multiply);
setSwitchr.get('multiply', 10, 20); // 200
- If a key already exists,
set
will update that value:
const updateSwitchr = switchr({
updateMe: 'old value'
});
updateSwitchr.set('updateMe', 'new value!');
updateSwitchr.get('updateMe'); // 'new value!'
Has
- The
has
method checks to see if a case exists:
const hasSwitchr = switchr({
foo: 'foo',
});
hasSwitchr.has('foo'); // true
hasSwitchr.has('bar'); // false
List
- The
list
method returns an array available keys:
const listSwitchr = switchr({
foo: 'foo',
bar: 'bar',
buzz: () => {
return 'buzz buzz';
},
default: 'default value'
});
listSwitchr.list(); // ['foo', 'bar', 'buzz', 'default'];
TypeScript Support
Switcher has full TypeScript support