Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import isLicensePlate from './lib/isLicensePlate';
import isStrongPassword from './lib/isStrongPassword';

import isVAT from './lib/isVAT';
import isNanoID from './lib/isNanoID';

const version = '13.15.15';

Expand Down Expand Up @@ -245,6 +246,7 @@ const validator = {
isLicensePlate,
isVAT,
ibanLocales,
isNanoID,
};

export default validator;
26 changes: 26 additions & 0 deletions src/lib/isNanoID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assertString from './util/assertString';
import merge from './util/merge';

function regexExp(options) {
const alphabets = options.alphabets;
const underscore = options.underscore ? '_' : '';
const hyphen = options.hyphen ? '-' : '';
const charClass = `${alphabets}${underscore}${hyphen}`;
const regExp = new RegExp(`^[${charClass}]{${options.length}}$`);
return regExp;
}

const default_options = {
alphabets: 'A-Za-z0-9',
length: '1,',
hyphen: true,
underscore: true,
};

export default function isNanoID(str, options) {
assertString(str);
options = merge(options, default_options);
return regexExp(options).test(str);
}

isNanoID('ABC456', { alphabets: 'ABCDEF123456' });
46 changes: 46 additions & 0 deletions test/validators/isDecimal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import test from '../testFunctions';

describe('isDecimal', () => {
it('Checks whether a number is Decimal or not', () => {
test({
validator: 'isDecimal',
valid: ['1.2', '+2.4', '-122.5'],
invalid: ['+-2.4'],
});
test({
validator: 'isDecimal',
args: [{ locale: 'ar', force_decimal: false, decimal_digits: '1' }],
valid: ['1٫2', '+2٫4', '-122٫5'],
invalid: ['+-2٫4', '2.4', '12٫8979', '1,2'],
});
test({
validator: 'isDecimal',
valid: ['1.2', '+2.4', '-122.5', '.5', '+.5', '-.5', '0.123', '1.234'],
invalid: ['', ' ', '+', '-', '1,2', '1.', ' 1.2', '1.2 ', '+-2.4'],
});
test({
validator: 'isDecimal',
args: [{ force_decimal: true }],
valid: ['1.0', '+.5', '-.5', '0.12', '.1'],
invalid: ['1', '2', '', '1.', ' 1.0'],
});
test({
validator: 'isDecimal',
args: [{ force_decimal: true, decimal_digits: '2' }],
valid: ['1.23', '+.12', '-0.12', '0.00'],
invalid: ['1.2', '1.234', '1', '.1', '.123'],
});
test({
validator: 'isDecimal',
args: [{ decimal_digits: '1,3' }],
valid: ['1.2', '1.23', '1.234', '.1', '.12', '.123'],
invalid: ['1.', '1.2345', '.1234'],
});
test({
validator: 'isDecimal',
args: [{ locale: 'ar' }],
valid: ['1٫2', '٫5', '+٫5', '-٫5'],
invalid: ['1.2', '1,2', '1٫', ' 1٫2'],
});
});
});
56 changes: 56 additions & 0 deletions test/validators/isNanoID.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import test from '../testFunctions';

describe('isNanoID', () => {
it('default options', () => {
test({
validator: 'isNanoID',
valid: ['Aiugiu645654sdf', 'D87D', '_-jj', 'A_B-C'],
invalid: ['+999', 'RGRY$', ' ', '*abc'],
});
});

it('custom alphabets', () => {
test({
validator: 'isNanoID',
args: [{ alphabets: 'ABCDEF123' }],
valid: ['ACABE', '123123', 'A1_C', 'F-1'],
invalid: [
'+999',
'RGRY$',
' ',
'Aiugiu645654sdf',
'D87D',
'aoihdof',
'_-jj',
'G12',
],
});
});

it('hyphen disabled', () => {
test({
validator: 'isNanoID',
args: [{ hyphen: false }],
valid: ['ACABE', '123123', '_jj', 'Aiugiu645654sdf', 'D87D'],
invalid: ['-999', 'A-B', 'RGRY$', ' '],
});
});

it('underscore disabled', () => {
test({
validator: 'isNanoID',
args: [{ underscore: false }],
valid: ['ACABE', '123123', '-jj', 'Aiugiu645654sdf', 'D87D'],
invalid: ['_123', 'A_B', 'RGRY$', ' '],
});
});

it('fixed length', () => {
test({
validator: 'isNanoID',
args: [{ length: 6 }],
valid: ['ABC123', 'a1B_9-'],
invalid: ['ABC12', 'ABC1234', 'ABC+12'],
});
});
});
Loading