PHP's assert_options in TypeScript

How to use

Install via yarn add locutus and import: import { assert_options } from 'locutus/php/info/assert_options'.

Or with CommonJS: const { assert_options } = require('locutus/php/info/assert_options')

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
1assert_options('ASSERT_CALLBACK')null

Dependencies

This function uses the following Locutus functions:

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

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

type AssertOptionValue = PhpRuntimeValue

export function assert_options(what: string, _value?: AssertOptionValue): string | number | null {
// discuss at: https://locutus.io/php/assert_options/
// original by: Brett Zamir (https://brett-zamir.me)
// example 1: assert_options('ASSERT_CALLBACK')
// returns 1: null

let iniKey: string
let defaultVal: number | null
switch (what) {
case 'ASSERT_ACTIVE':
iniKey = 'assert.active'
defaultVal = 1
break
case 'ASSERT_WARNING': {
iniKey = 'assert.warning'
defaultVal = 1
let msg = 'We have not yet implemented warnings for us to throw '
msg += 'in JavaScript (assert_options())'
throw new Error(msg)
}
case 'ASSERT_BAIL':
iniKey = 'assert.bail'
defaultVal = 0
break
case 'ASSERT_QUIET_EVAL':
iniKey = 'assert.quiet_eval'
defaultVal = 0
break
case 'ASSERT_CALLBACK':
iniKey = 'assert.callback'
defaultVal = null
break
default:
throw new Error('Improper type for assert_options()')
}

// I presume this is to be the most recent value, instead of the default value
const iniVal = ini_get(iniKey)
return iniVal === '' ? defaultVal : iniVal
}

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


Star