UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

52 lines (51 loc) 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ini_set = ini_set; const _phpRuntimeState_ts_1 = require("../_helpers/_phpRuntimeState.js"); const isIniScalar = (value) => typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean' || value === null; const isIniObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value); const isIniArray = (value) => Array.isArray(value) && value.every((item) => isIniEntryValue(item)); const isIniEntryValue = (value) => isIniScalar(value) || isIniObject(value) || isIniArray(value); const isIniValue = (value) => isIniEntryValue(value); function ini_set(varname, newvalue) { // discuss at: https://locutus.io/php/ini_set/ // parity verified: PHP 8.3 // original by: Brett Zamir (https://brett-zamir.me) // note 1: This will not set a global_value or access level for the ini item // example 1: ini_set('date.timezone', 'Asia/Hong_Kong') // example 1: ini_set('date.timezone', 'America/Chicago') // returns 1: 'Asia/Hong_Kong' const runtime = (0, _phpRuntimeState_ts_1.ensurePhpRuntimeState)(); const entry = runtime.ini[varname] ?? (runtime.ini[varname] = {}); const currentValue = entry.local_value; const oldval = typeof currentValue === 'undefined' ? undefined : isIniValue(currentValue) ? currentValue : undefined; let normalizedValue = newvalue; const lowerStr = String(newvalue).toLowerCase().trim(); if (newvalue === true || lowerStr === 'on' || lowerStr === '1') { normalizedValue = 'on'; } if (newvalue === false || lowerStr === 'off' || lowerStr === '0') { normalizedValue = 'off'; } const setArrayValue = () => { // Although these are set individually, they are all accumulated. if (typeof entry.local_value === 'undefined') { entry.local_value = [normalizedValue]; return; } if (Array.isArray(entry.local_value)) { entry.local_value.push(normalizedValue); return; } entry.local_value = [entry.local_value, normalizedValue]; }; switch (varname) { case 'extension': setArrayValue(); break; default: entry.local_value = normalizedValue; break; } return oldval; }