Skip to content

Commit 2866bb1

Browse files
authored
feat(isTime) add withOptionalSeconds option (#2521)
1 parent aacf9b5 commit 2866bb1

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Validator | Description
165165
**isUppercase(str)** | check if the string is uppercase.
166166
**isSlug(str)** | check if the string is of type slug.
167167
**isStrongPassword(str [, options])** | check if the string can be considered a strong password or not. Allows for custom requirements or scoring rules. If `returnScore` is true, then the function returns an integer score for the password rather than a boolean.<br/>Default options: <br/>`{ minLength: 8, minLowercase: 1, minUppercase: 1, minNumbers: 1, minSymbols: 1, returnScore: false, pointsPerUnique: 1, pointsPerRepeat: 0.5, pointsForContainingLower: 10, pointsForContainingUpper: 10, pointsForContainingNumber: 10, pointsForContainingSymbol: 10 }`
168-
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFormat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default'` or `'withSeconds'`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format.
168+
**isTime(str [, options])** | check if the string is a valid time e.g. [`23:01:59`, new Date().toLocaleTimeString()].<br/><br/> `options` is an object which can contain the keys `hourFormat` or `mode`.<br/><br/>`hourFormat` is a key and defaults to `'hour24'`.<br/><br/>`mode` is a key and defaults to `'default'`. <br/><br/>`hourFormat` can contain the values `'hour12'` or `'hour24'`, `'hour24'` will validate hours in 24 format and `'hour12'` will validate hours in 12 format. <br/><br/>`mode` can contain the values `'default', 'withSeconds', withOptionalSeconds`, `'default'` will validate `HH:MM` format, `'withSeconds'` will validate the `HH:MM:SS` format, `'withOptionalSeconds'` will validate `'HH:MM'` and `'HH:MM:SS'` formats.
169169
**isTaxID(str, locale)** | check if the string is a valid Tax Identification Number. Default locale is `en-US`.<br/><br/>More info about exact TIN support can be found in `src/lib/isTaxID.js`.<br/><br/>Supported locales: `[ 'bg-BG', 'cs-CZ', 'de-AT', 'de-DE', 'dk-DK', 'el-CY', 'el-GR', 'en-CA', 'en-GB', 'en-IE', 'en-US', 'es-AR', 'es-ES', 'et-EE', 'fi-FI', 'fr-BE', 'fr-CA', 'fr-FR', 'fr-LU', 'hr-HR', 'hu-HU', 'it-IT', 'lb-LU', 'lt-LT', 'lv-LV', 'mt-MT', 'nl-BE', 'nl-NL', 'pl-PL', 'pt-BR', 'pt-PT', 'ro-RO', 'sk-SK', 'sl-SI', 'sv-SE', 'uk-UA']`.
170170
**isURL(str [, options])** | check if the string is a URL.<br/><br/>`options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, require_host: true, require_port: false, require_valid_protocol: true, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false, allow_protocol_relative_urls: false, allow_fragments: true, allow_query_components: true, disallow_auth: false, validate_length: true }`.<br/><br/>`require_protocol` - if set to true isURL will return false if protocol is not present in the URL.<br/>`require_valid_protocol` - isURL will check if the URL's protocol is present in the protocols option.<br/>`protocols` - valid protocols can be modified with this option.<br/>`require_host` - if set to false isURL will not check if host is present in the URL.<br/>`require_port` - if set to true isURL will check if port is present in the URL.<br/>`allow_protocol_relative_urls` - if set to true protocol relative URLs will be allowed.<br/>`allow_fragments` - if set to false isURL will return false if fragments are present.<br/>`allow_query_components` - if set to false isURL will return false if query components are present.<br/>`validate_length` - if set to false isURL will skip string length validation. `max_allowed_length` will be ignored if this is set as `false`.<br/>`max_allowed_length` - if set isURL will not allow URLs longer than the specified value (default is 2084 that IE maximum URL length).
171171
**isULID(str)** | check if the string is a [ULID](https://github.com/ulid/spec).

src/lib/isTime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ const formats = {
99
hour24: {
1010
default: /^([01]?[0-9]|2[0-3]):([0-5][0-9])$/,
1111
withSeconds: /^([01]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/,
12+
withOptionalSeconds: /^([01]?[0-9]|2[0-3]):([0-5][0-9])(?::([0-5][0-9]))?$/,
1213
},
1314
hour12: {
1415
default: /^(0?[1-9]|1[0-2]):([0-5][0-9]) (A|P)M$/,
1516
withSeconds: /^(0?[1-9]|1[0-2]):([0-5][0-9]):([0-5][0-9]) (A|P)M$/,
17+
withOptionalSeconds: /^(0?[1-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))? (A|P)M$/,
1618
},
1719
};
1820

test/validators.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13988,6 +13988,35 @@ describe('Validators', () => {
1398813988
'009:50:01',
1398913989
],
1399013990
});
13991+
test({
13992+
validator: 'isTime',
13993+
args: [{ hourFormat: 'hour24', mode: 'withOptionalSeconds' }],
13994+
valid: [
13995+
'23:59:59',
13996+
'00:00:00',
13997+
'9:50:01',
13998+
'00:00',
13999+
'23:59',
14000+
'9:00',
14001+
],
14002+
invalid: [
14003+
'',
14004+
null,
14005+
undefined,
14006+
23,
14007+
'01:00:01 PM',
14008+
'13:00:',
14009+
'00',
14010+
'26',
14011+
'00;01',
14012+
'0 :09',
14013+
'59:59:59',
14014+
'24:00:00',
14015+
'00:59:60',
14016+
'99:99:99',
14017+
'009:50:01',
14018+
],
14019+
});
1399114020
test({
1399214021
validator: 'isTime',
1399314022
args: [{ hourFormat: 'hour12' }],
@@ -14047,6 +14076,38 @@ describe('Validators', () => {
1404714076
'009:50:01',
1404814077
],
1404914078
});
14079+
test({
14080+
validator: 'isTime',
14081+
args: [{ hourFormat: 'hour12', mode: 'withOptionalSeconds' }],
14082+
valid: [
14083+
'12:59:59 PM',
14084+
'2:34:45 AM',
14085+
'7:00:00 AM',
14086+
'12:59 PM',
14087+
'12:59 AM',
14088+
'01:00 PM',
14089+
'01:00 AM',
14090+
'7:00 AM',
14091+
],
14092+
invalid: [
14093+
'',
14094+
null,
14095+
undefined,
14096+
23,
14097+
'01:00: 1 PM',
14098+
'13:00:',
14099+
'00',
14100+
'26',
14101+
'00;01',
14102+
'0 :09',
14103+
'59:59:59',
14104+
'24:00:00',
14105+
'00:59:60',
14106+
'99:99:99',
14107+
'9:50:01',
14108+
'009:50:01',
14109+
],
14110+
});
1405014111
});
1405114112
it('should be valid license plate', () => {
1405214113
test({

0 commit comments

Comments
 (0)