PHP's isset in TypeScript

How to use

Install via yarn add locutus and import: import { isset } from 'locutus/php/var/isset'.

Or with CommonJS: const { isset } = require('locutus/php/var/isset')

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
1isset( undefined, true)false
2isset( 'Kevin van Zonneveld' )true

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

import type { PhpRuntimeValue } from '../_helpers/_phpTypes.ts'

type IssetValue = PhpRuntimeValue

export function isset(...values: IssetValue[]): boolean {
// discuss at: https://locutus.io/php/isset/
// original by: Kevin van Zonneveld (https://kvz.io)
// improved by: FremyCompany
// improved by: Onno Marsman (https://twitter.com/onnomarsman)
// improved by: Rafał Kukawski (https://blog.kukawski.pl)
// example 1: isset( undefined, true)
// returns 1: false
// example 2: isset( 'Kevin van Zonneveld' )
// returns 2: true

if (values.length === 0) {
throw new Error('Empty isset')
}

for (const value of values) {
if (typeof value === 'undefined' || value === null) {
return false
}
}

return true
}

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 var functions


Star