1.25.0 • Published 11 months ago

werkstatt v1.25.0

Weekly downloads
4
License
ISC
Repository
github
Last release
11 months ago

Werkstatt

šŸ›  Useful functions to encapsulate common scenarios.

travis build

As described here, is good to encapsulate conditionals to make our code more readable, reusable and avoid ambiguities. Also to avoid potential bugs due to some javascript features are error-prone:

let name = null;
typeof name; // "object"

Clearly, null is not an object. More of that šŸ‘‰šŸ¼here.

This is then, a package that encapsulates conditionals, but also other util functions.

āš ļø WARNING:

This is also a proof of concept. Some of the functions' names may not make all the sense to you or also may be many breaking changes.

šŸ“¦ Install

npm

npm install -s werkstatt

yarn

yarn add werkstatt

browser

<script src="https://unpkg.com/werkstatt@1.16.0/dist/index.umd.min.js"></script>

<script>
  const { isEmail, isNull, areEqual } = werkstatt;

  console.log(isEmail("asdf")); // -> false
  console.log(isNull(3)); // -> false
  console.log(areEqual(6, 6, 6, 6)); // -> true
</script>

✨ Features

Number

Arguments
argumenttypedescriptionreturns
valuenumberwill be tested if it is 0 or notboolean
const { isZero } = require("werkstatt");

isZero(7); // -> false
isZero(0); // -> true
Arguments
argumenttypedescriptionreturns
valuesnumbereither an array of numbers or n argsnumber
const { add } = require("werkstatt");

add(3, 6, 11); // -> 20
const numbers = [1, 2, 3];
add(...numbers); // -> 6

// or just pass the array
add(numbers); // -> 6
Arguments
argumenttypedescriptionreturns
valuesnumbernumbers that will be subtrtactednumber
const { subtract } = require("werkstatt");

subtract(6, 3); // -> 3

NOTE: currently it only supports two numbers as paremeters.

Arguments
argumenttypedescriptionreturns
dividendnumberthe dividend of the operationnumber
dividernumberthe divider of the operationnumber
const { divide } = require("werkstatt");

divide(100, 2); // -> 50
divide(10, 5); // -> 2
Arguments
argumenttypedescriptionreturns
valueanywill be tested if it is number or notboolean
const { isNumber } = require("werkstatt");

isNumber(54); // -> true
isNumber({ hola: "adios" }); // -> false
isNumber([]); // -> false
isNumber(""); // -> false
isNumber(3); // -> true
isNumber(true); // -> false

Or use the .number getter exposed by the is() function.

const { is } = require("werkstatt");

is(54).number; // -> true
is([3]).number; // -> false

NOTE: this is an implementation of is-number package.

Arguments
argumenttypedescriptionreturns
valuenumberwill be tested if it is odd number or notboolean
const { isOdd } = require("werkstatt");

isOdd(7); // -> true
isOdd(4); // -> false

NOTE: this is an implementation of is-odd package.

Arguments
argumenttypedescriptionreturns
valuenumberwill be tested if it is even number or notboolean
const { isEven } = require("werkstatt");

isEven(7); // -> false
isEven(4); // -> true

NOTE: this is an implementation of is-even package.

Arguments
argumenttypedescriptionreturns
valuenumberwill be tested if it is negative number or notboolean
const { isNegative } = require("werkstatt");

isNegative(-54); // -> true
isNegative(4); // -> false
Arguments
argumenttypedescriptionreturns
firstArgumentnumberfirst value to be evaluatedboolean
secondArgumentnumbersecond value to be evaluatedboolean
const { isGreaterThan } = require("werkstatt");

isGreaterThan(100, 50); // -> true
isGreaterThan(1, 50); // -> false
Arguments
argumenttypedescription
valuenumbernumber to convert
precisionnumberdesired amount of decimals
const { toFixed } = require("werkstatt");

toFixed(3.14, 4); // -> '3.1400'
toFixed(5.1346, 3); // -> '5.135'
Arguments
argumenttypedescription
valuearray of numbers or several argswhere to look for the lowest value
const { min } = require("werkstatt");

min(264, 736, 223, 979, 124); // -> 124
min([543, 333, 22, 1865, 976]); // -> 22
Arguments
argumenttypedescription
valuearray of numbers or several argswhere to look for the highest value
const { max } = require("werkstatt");

max(264, 736, 223, 979, 124); // -> 979
max([543, 333, 22, 1865, 976]); // -> 1865
Arguments
argumenttypedescription
valuenumberfloat number where decimales will be removed
const { truncate } = require("werkstatt");

truncate(123.4567); // -> 123
Arguments
argumenttypedescription
lowernumberlower number desired
uppernumberupper number desired
precisionnumberamount of decimals desired
const { random } = require("werkstatt");

random(5, 10, 2); // -> 9.32

Float

Arguments
argumenttype
numberfloat
const { roundUp } = require("werkstatt");

roundUp(3.2); // -> 4
Arguments
argumenttype
numberfloat
const { round } = require("werkstatt");

round(5.95); // -> 6
round(5.5); // -> 6
round(5.05); // -> 5
Arguments
argumenttype
numberfloat
const { roundDown } = require("werkstatt");

roundDown(3.8); // -> 3
Arguments
argumenttypedescriptionreturns
valuenumber, floatwill be tested if is or not floatboolean
const { isFloat } = require("werkstatt");

isFloat(6); // -> false
isFloat(6.5); // -> true

Or use the .float getter exposed by the is() function.

const { is } = require("werkstatt");

is(5).float; // -> false
is(6.5).float; // -> true

String

Arguments
argumenttypedescriptionreturns
valuestringstring to capitalize first letterstring
const { capitalizeFirstLetter } = require("werkstatt");

capitalizeFirstLetter("hola"); // -> 'Hola'
capitalizeFirstLetter("adios"); // -> 'Adios'

NOTE: this is an implementation of a Flavio's function

Arguments
argumenttypedescriptionreturns
valuestringwill be tested if it satisfies an email formatboolean
const { isEmail } = require("werkstatt");

isEmail("a@a.c"); // -> false
isEmail("a@a.co"); // -> true

Or use the .email getter exposed by the is() function.

const { is } = require("werkstatt");

is("a@a.c").email; // -> false
is("a@a.co").email; // -> true

Best regex found out there.

Arguments
argumenttypedescriptionreturns
valueanywhether or not the value is a stringboolean
const { isString } = require("werkstatt");

isString("Hola"); // -> true
isString([3]); // -> false

Or use the .string getter exposed by the is() function.

const { is } = require("werkstatt");

is("Hola").string; // -> true
is([3]).tring; // -> false
Arguments
argumenttypedescriptionreturns
valuestringstring to be slugifiedstring
const { slufigy } = require("werkstatt");

slufigy("Hola Mundo"); // -> hola-mundo
slufigy("Verbos modales en ingles"); // -> verbos-modales-en-ingles 
Arguments
argumenttypedescriptionreturns
valueanyvalue to be converted to stringstring
const { toString } = require("werkstatt");

toString(123); // -> "123"
toString({ greeting: 'hola' }); // -> '{"greeting":"hola"}'

Boolean

Whenever JavaScript expects a boolean value (e.g. for the condition of an if statement), any value can be used. It will be interpreted as either true or false. The following values are interpreted as false:

  • undefined, null
  • Boolean: false
  • Number: -0, NaN
  • String: ''

Speaking JavaScript by Alex Rauschmayer

That means that those values tend to to be false. So if you pass as parameter to isTruthy function any of those values, it will return false. All other values are considered true.

const { isTruthy } = require("werkstatt");

isTruthy(3); // -> true
isTruthy({}); // -> true

isTruthy(undefined); // -> false
isTruthy(null); // -> false
isTruthy(false); // -> false
isTruthy(Number("hola")); // -> false
isTruthy(0); // -> false
isTruthy(-0); // -> false
isTruthy(""); // -> false

Exactly the opposite of isTruthy.

const { isFalsy } = require("werkstatt");

isFalsy(3); // -> false
isFalsy(null); // -> true

Array

Arguments
argumenttypedescriptionreturns
valuearraywill order the list in ascending modearray (ordened)
const { orderAsc } = require("werkstatt");

orderAsc([8, 10, 6]); // -> [6, 8, 10]

NOTE: this is an implementation of quicksort algorithm

Arguments
argumenttypedescriptionreturns
valuearray, string, jsonlength of the passed argumentnumber
const { lengthOf } = require("werkstatt");

lengthOf([8, 10, 6]); // -> 3
Arguments
argumenttypedescriptionreturns
valuearray, string, jsontest if the first argument has the desired length (that specified in the second argument)boolean
const { isLengthOf } = require("werkstatt");

isLengthOf([8, 10, 6], 3); // -> true
isLengthOf("hola", 0); // -> false
isLengthOf({ name: "Jorge", lasName: "Guerra" }, 2); // -> true

Or use the .lengthOf prop exposed by the is() function.

const { is } = require("werkstatt");

is(2).lengthOf([1, 2]); // -> true
Arguments
argumenttypedescription
arrayanywhere the element will be inserted.
indexanyat which the element will be inserted.
elementToInsertanyelement to insert in the array.
const { insertAt } = require("werkstatt");

insertAt([1, 2, 3], 1, 4); // -> [1, 4, 2, 3]
Arguments
argumenttypedescriptionreturns
valuesnumberarray to testboolean
const { isArrayOfNumbers } = require("werkstatt");

isArrayOfNumbers([3, 6, 11, "hola"]); // -> false
isArrayOfNumbers([1, 2, 3]); // -> true
Arguments
argumenttypedescriptionreturns
arrayLikeObject/NodeListanyobject to convertarray
const { toArray } = require("werkstatt");

function testToArray() {
  console.log(arguments); // array like object -> [Arguments] { '0': 1, '1': 2, '2': 3 }
  return toArray(arguments);
}

testToArray(1, 2, 3); // -> [ 1, 2, 3 ]
Arguments
argumenttypedescriptionreturns
valueanywhether or not the value is an arrayboolean
const { isArray } = require("werkstatt");

isArray("Hola"); // -> false
isArray([3]); // -> true
Arguments
argumenttypedescriptionreturns
arrayarrayArray to be processedarray
const { uniquify } = require("werkstatt");

const shoes = [
  { id: 1, name: "nikesb" },
  { id: 1, name: "nikesb" },
  { id: 2, name: "lakai" },
  { id: 2, name: "lakai" },
  { id: 3, name: "etnies" },
];

const unique = uniquify(shoes, (a, b) => a.id === b.id);

/*

[
  { id: 1, name: "nikesb" },
  { id: 2, name: "lakai" },
  { id: 3, name: "etnies" },
]

*/
Arguments
argumenttypedescription
arrayarrayArray to be processed
transformfuncfunction to apply to element in the array
const { map } = require("werkstatt");

const numbers = [1, 2, 3];
map(numbers, (x) => x * 2); // -> [2, 4, 6]
Arguments
argumenttypedescription
arrayarrayArray to be processed
const { last } = require("werkstatt");

const numbers = [1, 2, 3];
last(numbers); // -> [3]
Arguments
argumenttypedescription
arrayarrayArray to be processed
const { join } = require("werkstatt");

const array = ['a', 'b', 'c'];
join(array); // -> abc
join(array, '-'); // -> a-b-c
Arguments
argumenttypedescription
arrayarrayArray to be processed
const { union } = require("werkstatt");

union(['a', 'b', 'c'], ['a', 'z', 'x']); // -> ['a', 'b', 'c', 'z', 'x']
Arguments
argumenttypedescription
arrayarrayArray to be processed
const { intersection } = require("werkstatt");

intersection(['a', 'b', 'c'], ['a', 'z', 'x']); // -> ['a']

Object

Arguments
argumenttype
targetobject
sourceobject
const { mergeDeep } = require("werkstatt");

const obj1 = {
  a: 1,
  b: 1,
  c: { x: 1, y: 1 },
  d: [1, 1],
};

const obj2 = {
  b: 2,
  c: { y: 2, z: 2 },
  d: [2, 2],
  e: 2,
};

mergeDeep(obj1, obj2);

/*

{ 
  a: 1, 
  b: 2, 
  c: { x: 1, y: 2, z: 2 }, 
  d: [ 1, 1, 2, 2 ], 
  e: 2 
};
*/

Author: jhildenbiddle

Arguments
argumenttype
objany
const { isObject } = require("werkstatt");

const obj1 = {
  a: 1,
  b: 1,
  c: { x: 1, y: 1 },
  d: [1, 1],
};

isObject(obj1); // -> true

Other

Arguments
argumenttypedescriptionreturns
valueanywill get the type of a passed valuestring
const { typeOf } = require("werkstatt");

typeOf(6.5); // -> float
typeOf([]); // -> array
typeOf({}); // -> object
typeOf(null); // -> 'null'
typeOf(undefined); // -> 'undefined'
typeOf("undefined"); // -> 'string'
typeOf(true); // -> 'boolean'
typeOf(() => {}); // -> 'function'
typeOf(6); // -> number
Arguments
argumenttypedescriptionreturns
n amountanyargs to compareboolean
const { areEqual } = require("werkstatt");

areEqual(100, 2); // -> false

var name;
areEqual(typeOf(name), "undefined"); // -> true

const numbers = [4, 3, 5, 7, 3, 9];
areEqual(...numbers); // -> false

const ages = [9, 9, 9, 9, 9];
areEqual(...ages); // -> true

NOTE: This function supports primitive values only because objects are not compared by value but by reference.

Arguments
argumenttypedescriptionreturns
valueanywill be tested if is undefined or notboolean
const { isUndefined } = require("werkstatt");

isUndefined(); // -> true
isUndefined("a@a.co"); // -> false
Arguments
argumenttypedescriptionreturns
valueanywill be tested if is or not definedboolean
const { isDefined } = require("werkstatt");

isDefined(100); // -> true
var name;
isDefined(name); // -> false

var age = null;
isDefined(age); // -> false
isDefined({}); // -> true
Arguments
argumenttype
paramany
const { isEmpty } = require("werkstatt");

isEmpty({}); // -> true
isEmpty({ hola: "adios" }); // -> false
isEmpty([]); // -> true
isEmpty(""); // -> true
isEmpty(3); // -> true
isEmpty(true); // -> true

Note: isEmpty currently supports array, object and string only.

Arguments
argumenttypedescriptionreturns
firstArgumentnumberfirst value to be evaluatedboolean
secondArgumentnumbersecond value to be evaluatedboolean
const { has } = require("werkstatt");

has([3, 5], 3); // -> true
has(["Hola", "adios"], "true"); // -> false
has("Jorge", "e"); // -> true
Arguments
argumenttypedescriptionreturns
valueanyvalue to be evaluatedboolean
const { isNull } = require("werkstatt");

var name = null;
isNull(name); // -> true
isNull("Hola"); // -> false
Arguments
argumenttypedescriptionreturns
argsarrayarguments to match to a specific typeboolean
const { every } = require("werkstatt");

every("adios" === "adios", "hola" === "hola").is.true; // -> true
every("adios", "hola").is.string; // -> true
every(1, 2).is.number; // -> true
Arguments
argumenttypedescriptionreturns
fnsarrayfunctions to be executedfunction
const { compose } = require("werkstatt");

const h = (n) => n / 2;
const g = (n) => n + 1;
const f = (n) => n * 2;

compose(f, g, h)(20); // -> 22
Arguments
argumenttypedescriptionreturns
itemarray, objectitem on where to remove fromcopy of item with props or values removed
const { removeFrom } = require("werkstatt");

const object = {
  name: "Jorge",
  age: 20,
  sex: "M",
};
//  pass an array of props
const props = ["name", "sex"];
const newObject = removeFrom(object, props);

// or one prop as string
const newObject = removeFrom(object, "name");

// pass an array of values
const array = ["red", "blue", "pink"];
const values = ["blue", "red"];
const newArray = removeFrom(array, values);

// or one value as string
const array = ["red", "blue"];
const value = "blue";
const newArray = removeFrom(array, value);

more coming soon ✨

šŸ™ŒšŸ½ Contribute

  1. Fork and clone the repo
  2. Run npm install to install dependencies
  3. Create a branch for your PR with git checkout -b your-branch-name

To keep master branch pointing to remote repository and make pull requests from branches on your fork. To do this, run:

git remote add upstream https://github.com/sk8guerra/werkstatt.git
git fetch upstream
git branch --set-upstream-to=upstream/master master
  1. Make your pull request 🄳
1.25.0

11 months ago

1.24.0

11 months ago

1.23.0

3 years ago

1.22.0

3 years ago

1.21.1

4 years ago

1.21.2

4 years ago

1.21.3

4 years ago

1.21.0

4 years ago

1.19.0

4 years ago

1.20.0

4 years ago

1.18.24

4 years ago

1.18.23

4 years ago

1.18.22

4 years ago

1.18.21

4 years ago

1.18.20

4 years ago

1.18.19

4 years ago

1.18.18

4 years ago

1.18.12

4 years ago

1.18.16

4 years ago

1.18.15

4 years ago

1.18.14

4 years ago

1.18.13

4 years ago

1.18.17

4 years ago

1.18.9

4 years ago

1.18.8

4 years ago

1.18.11

4 years ago

1.18.10

4 years ago

1.18.5

4 years ago

1.18.4

4 years ago

1.18.3

4 years ago

1.18.2

4 years ago

1.18.7

4 years ago

1.18.6

4 years ago

1.18.1

4 years ago

1.18.0

4 years ago

1.17.0

5 years ago

1.16.0

5 years ago

1.15.0

5 years ago

1.14.0

5 years ago

1.13.1

5 years ago

1.13.0

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.0

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago