PHP's ctype_digit in TypeScript

✓ Verified: PHP 8.3
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
Rosetta Stone: c/isdigit

How to use

Install via yarn add locutus and import: import { ctype_digit } from 'locutus/php/ctype/ctype_digit'.

Or with CommonJS: const { ctype_digit } = require('locutus/php/ctype/ctype_digit')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1ctype_digit('150')true

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to PHP's ctype_digit looks like.

import { getCtypePattern } from '../_helpers/_ctypePattern.ts'
import { setlocale } from '../strings/setlocale.ts'

export function ctype_digit(text: string): boolean {
// discuss at: https://locutus.io/php/ctype_digit/
// parity verified: PHP 8.3
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: ctype_digit('150')
// returns 1: true

if (typeof text !== 'string') {
return false
}
setlocale('LC_ALL', 0)

const pattern = getCtypePattern('dg')
return pattern instanceof RegExp ? pattern.test(text) : false
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


« More PHP ctype functions


Star