1.0.3 • Published 2 years ago

@surveyplanet/utilities v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

SurveyPlanet Typescript utility library

A collection of utility functions.

Install

npm install @surveyplanet/utilities

Functions

Delay

Delay the call stack for a specified number of milliseconds

Example

import { delay } from '@surveyplanet/utilities';
const someFunction = async (checked) => {
	console.log(new Date());
	await delay(100);
	console.log(new Date());
};

Properties

PropertyTypeDescription
ms=0NumberThe total number of milliseconds to delay for. Operates as a nextTick if not provided

Validate

Validate form inputs

Example

<script>
	import { validate } from '@surveyplanet/utilities';

	function submit () {
	  const errors = validate();
	  if (!errors) { fetch(...) }
	}
</script>

<label for="email">Email</label>
<input
	type="email"
	id="email"
	name="email"
	data-validate-rules="required,email"
	data-validate-message="Hey! you need to use a proper email dude." />

<label for="password">Password</label>
<input
	type="password"
	id="password"
	name="password"
	data-validate-rules="required,minLength[8],hasSpecialChar,hasNumber,hasUpper,hasLower" />

<label for="confirm-password">Confirm Password</label>
<input
	type="confirm-password"
	id="confirm-password"
	name="confirm-password"
	data-validate-rules="matches[password]" />

Properties

PropertyTypeDescription
inputHTMLInputElement | HTMLSelectElement | nullThe input to validate. Must have data-validate-rules attribute with the appropriate rules value. See below for validation rules. If no input are provided all input and select elements with the data-validate-rules attribute are validated automatically.

Dataset attributes

PropertyDescription
data-validate-rulesA comma separated list of rules to use for validation. See validation rules below.
data-validate-messageA custom error message instead of the default one. Use %l to add the input label to your element and %p to the validation property. For example if you use minLength[8] %p is 8.
data-validate-hide-errorsDon't automatically show the validation error under the text box. You can handel them yourself this way.

Validation rules

PropertyDescription
requiredMust not be empty.
matches[number]Must match another field value.
urlMust be a valid url.
emailMust be a valid email address.
emailsMust be a comma separated list of valid email addresses.
minLength[number]Must be at least X characters long.
maxLength[number]Must be no longer than X characters.
exactLength[number]Must be exactly X characters long.
greaterThan[number]Must be greater than X.
lessThan[number]Must be less than X.
equals[number]Must be equal to X.
alphaCan only contain alphabetical characters (A-z).
alphaNumericCan only contain alpha-numeric characters (A-z, 0-9).
alphaDashCan only contain alpha-numeric characters, underscores, or dashes.
numericMust be a whole (non-negative) number.
integerMust be an integer; either positive or negative.
decimalMust be a valid integer or decimal consist of two parts: an integer and a fraction separated by a decimal point.
ipMust be a valid IP address.
base64Must be a base64 string.
phoneMust be a valid phone number.
cvcMust be a valid credit card cvc.
creditCardMust be a valid credit card number.
fileType[string]Must be a comma separated list of file types e.g.: gif,png,jpg.
hasSpecialCharMust contain a special character e.g.: $&+,:;=?@#|'"<>.^*()%!-.
hasNumberMust contain a number.
hasUpperMust contain an upper case letter.
hasLowerMust contain a lower case letter.

Tests

Vitest is used for unit testing. The coverage is also done through vitest, using c8.

Commands

  • test: runs vitest test runner
  • test:watch: runs vitest test runner in watch mode
  • test:coverage: runs vitest test runner and generates coverage reports

Format & lint

This library relies on the combination of eslint — through typescript-eslint for linting and prettier for formatting. It also uses cspell to ensure spelling

Commands

  • format: runs prettier with automatic fixing
  • format:check: runs prettier without automatic fixing (used in CI)
  • lint: runs eslint with automatic fixing
  • lint:check: runs eslint without automatic fixing (used in CI)
  • spell: runs spell checker

Releasing

Under the hood, this library uses semantic-release. The goal is to avoid manual release process. Using semantic-release will automatically create a github release (hence tags) as well as an npm release. Based on your commit history, semantic-release will automatically create a patch, feature or breaking release.

Commands

  • cz: interactive CLI that helps you generate a proper git commit message, using commitizen
  • semantic-release: triggers a release (used in CI)
1.0.0-beta.6

2 years ago

1.0.0-beta.5

2 years ago

1.0.0-beta.4

2 years ago

1.0.0-beta.3

2 years ago

1.0.0-beta.2

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.0-beta.1

2 years ago

1.0.1

2 years ago

0.0.1-alpha.0

2 years ago

1.0.0

2 years ago