Skip to content

Commit

Permalink
Bulk trim text values
Browse files Browse the repository at this point in the history
Closes #31
  • Loading branch information
takayukister committed Jul 25, 2024
1 parent f67a4d9 commit fcda4fe
Show file tree
Hide file tree
Showing 18 changed files with 41 additions and 48 deletions.
5 changes: 2 additions & 3 deletions rules/date.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ValidationError } from '../error';

export const date = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

// https://html.spec.whatwg.org/multipage/input.html#date-state-(type=date)
const isValidDateString = text => {
text = text.trim();

if ( ! /^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( text ) ) {
return false;
}
Expand Down
5 changes: 3 additions & 2 deletions rules/dayofweek.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ValidationError } from '../error';

export const dayofweek = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const convertToIso8601 = jsDow => ( 0 === jsDow ) ? 7 : jsDow;

const isAcceptableValue = value => {
const date = new Date( value.trim() );
const date = new Date( value );
const day = convertToIso8601( date.getDay() );

return this.accept?.some(
Expand Down
5 changes: 2 additions & 3 deletions rules/email.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ValidationError } from '../error';

export const email = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

// Equivalent to is_email()
// https://developer.wordpress.org/reference/functions/is_email/
const isValidEmail = text => {
text = text.trim();

if ( text.length < 6 ) {
return false;
}
Expand Down
5 changes: 3 additions & 2 deletions rules/enum.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { ValidationError } from '../error';

export const enumeration = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const isAcceptableValue = value => this.accept?.some(
acceptableValue => value.trim() === String( acceptableValue )
acceptableValue => value === String( acceptableValue )
);

if ( ! values.every( isAcceptableValue ) ) {
Expand Down
5 changes: 2 additions & 3 deletions rules/maxdate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ValidationError } from '../error';

export const maxdate = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const isAcceptableDate = text => {
text = text.trim();

if (
/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( text ) &&
/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( this.threshold ) &&
Expand Down
5 changes: 2 additions & 3 deletions rules/maxitems.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ValidationError } from '../error';

export const maxitems = function ( formDataTree ) {
const values = formDataTree.getAll( this.field ).filter(
val => '' !== val.trim()
);
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

if ( parseInt( this.threshold ) < values.length ) {
throw new ValidationError( this );
Expand Down
5 changes: 3 additions & 2 deletions rules/maxlength.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ValidationError } from '../error';

export const maxlength = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

let totalLength = 0;

values.forEach( text => {
if ( 'string' === typeof text ) {
totalLength += text.trim().length;
totalLength += text.length;
}
} );

Expand Down
3 changes: 2 additions & 1 deletion rules/maxnumber.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ValidationError } from '../error';

export const maxnumber = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const isAcceptableNumber = text => {
if ( parseFloat( this.threshold ) < parseFloat( text ) ) {
Expand Down
5 changes: 2 additions & 3 deletions rules/mindate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ValidationError } from '../error';

export const mindate = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const isAcceptableDate = text => {
text = text.trim();

if (
/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( text ) &&
/^[0-9]{4,}-[0-9]{2}-[0-9]{2}$/.test( this.threshold ) &&
Expand Down
5 changes: 2 additions & 3 deletions rules/minitems.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ValidationError } from '../error';

export const minitems = function ( formDataTree ) {
const values = formDataTree.getAll( this.field ).filter(
val => '' !== val.trim()
);
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

if ( values.length < parseInt( this.threshold ) ) {
throw new ValidationError( this );
Expand Down
5 changes: 3 additions & 2 deletions rules/minlength.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { ValidationError } from '../error';

export const minlength = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

let totalLength = 0;

values.forEach( text => {
if ( 'string' === typeof text ) {
totalLength += text.trim().length;
totalLength += text.length;
}
} );

Expand Down
3 changes: 2 additions & 1 deletion rules/minnumber.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ValidationError } from '../error';

export const minnumber = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const isAcceptableNumber = text => {
if ( parseFloat( text ) < parseFloat( this.threshold ) ) {
Expand Down
5 changes: 2 additions & 3 deletions rules/number.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ValidationError } from '../error';

export const number = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

// https://html.spec.whatwg.org/multipage/input.html#number-state-(type=number)
const isValidFloatingPointNumber = text => {
text = text.trim();

if ( /^[-]?[0-9]+(?:[eE][+-]?[0-9]+)?$/.test( text ) ) {
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions rules/required.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { ValidationError } from '../error';

export const required = function ( formDataTree ) {
const values = formDataTree.getAll( this.field ).filter(
val => '' !== val.trim()
);
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

if ( 0 === values.length ) {
throw new ValidationError( this );
Expand Down
5 changes: 2 additions & 3 deletions rules/stepnumber.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ValidationError } from '../error';

export const stepnumber = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const base = parseFloat( this.base );
const interval = parseFloat( this.interval );
Expand All @@ -11,8 +12,6 @@ export const stepnumber = function ( formDataTree ) {
}

const matchesStep = text => {
text = text.trim();

const remainder = ( parseFloat( text ) - base ) % interval;

if (
Expand Down
4 changes: 2 additions & 2 deletions rules/tel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ValidationError } from '../error';

export const tel = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const isTelephoneNumber = text => {
text = text.trim();
text = text.replaceAll( /[()/.*#\s-]+/g, '' );

return /^[+]?[0-9]+$/.test( text );
Expand Down
5 changes: 3 additions & 2 deletions rules/time.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { ValidationError } from '../error';

export const time = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

// https://html.spec.whatwg.org/multipage/input.html#time-state-(type=time)
const isValidTimeString = text => {
const pattern = /^([0-9]{2})\:([0-9]{2})(?:\:([0-9]{2}))?$/;
const matches = text.trim().match( pattern );
const matches = text.match( pattern );

if ( ! matches ) {
return false;
Expand Down
9 changes: 2 additions & 7 deletions rules/url.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { ValidationError } from '../error';

export const url = function ( formDataTree ) {
const values = formDataTree.getAll( this.field );
const values = formDataTree.getAll( this.field )
.map( val => val.trim() ).filter( val => '' !== val );

const isAbsoluteUrl = text => {
text = text.trim();

if ( '' === text ) {
return false;
}

try {
const urlObj = new URL( text );
const protocol = urlObj.protocol.replace( /:$/, '' );
Expand Down

0 comments on commit fcda4fe

Please sign in to comment.