// warning: This file is auto generated by `yarn build:tests` // Do not edit by hand! import { createRequire } from 'node:module' import { describe, it, expect } from 'vitest' process.env.TZ = 'UTC' const ini_set = require('../../../../src/php/info/ini_set.ts').ini_set const ini_get = require('../../../../src/php/info/ini_get.ts').ini_get const __locutus_source_fn = require('../../../../src/php/array/usort.ts').usort const __locutus_source_module_url = new URL("../../../../src/php/array/usort.ts", import.meta.url) const __locutus_source_require = createRequire(__locutus_source_module_url) const __locutus_func_name = "usort" const __locutus_module_js_code = "\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.usort = usort;\nconst _callbackResolver_ts_1 = require(\"../_helpers/_callbackResolver.ts\");\nconst _phpRuntimeState_ts_1 = require(\"../_helpers/_phpRuntimeState.ts\");\nfunction usort(inputArr, sorter) {\n // discuss at: https://locutus.io/php/usort/\n // original by: Brett Zamir (https://brett-zamir.me)\n // improved by: Brett Zamir (https://brett-zamir.me)\n // note 1: This function deviates from PHP in returning a copy of the array instead\n // note 1: of acting by reference and returning true; this was necessary because\n // note 1: IE does not allow deleting and re-adding of properties without caching\n // note 1: of property position; you can set the ini of \"locutus.sortByReference\" to true to\n // note 1: get the PHP behavior, but use this only if you are in an environment\n // note 1: such as Firefox extensions where for-in iteration order is fixed and true\n // note 1: property deletion is supported. Note that we intend to implement the PHP\n // note 1: behavior by default if IE ever does allow it; only gives shallow copy since\n // note 1: is by reference in PHP anyways\n // example 1: var $stuff = {d: '3', a: '1', b: '11', c: '4'}\n // example 1: usort($stuff, function (a, b) { return (a - b) })\n // example 1: var $result = $stuff\n // returns 1: {0: '1', 1: '3', 2: '4', 3: '11'}\n const valArr = [];\n let k = '';\n let i = 0;\n let sortByReference = false;\n let populateArr = {};\n const normalizedSorter = typeof sorter === 'string' ? [this, sorter] : sorter;\n let comparator;\n try {\n const resolved = (0, _callbackResolver_ts_1.resolvePhpCallable)(normalizedSorter, {\n invalidMessage: 'usort(): Invalid callback',\n missingScopeMessage: (scopeName) => 'Object not found: ' + scopeName,\n });\n comparator = (a, b) => Number(resolved.fn.apply(resolved.scope, [a, b]));\n }\n catch (_error) {\n return false;\n }\n const runtime = (0, _phpRuntimeState_ts_1.ensurePhpRuntimeState)();\n const iniVal = String(runtime.ini['locutus.sortByReference']?.local_value ?? '') || 'on';\n sortByReference = iniVal === 'on';\n populateArr = sortByReference ? inputArr : populateArr;\n for (k in inputArr) {\n // Get key and value arrays\n if (inputArr.hasOwnProperty(k)) {\n const value = inputArr[k];\n if (typeof value !== 'undefined') {\n valArr.push(value);\n }\n if (sortByReference) {\n delete inputArr[k];\n }\n }\n }\n try {\n if (typeof comparator !== 'function') {\n return false;\n }\n valArr.sort(comparator);\n }\n catch (_e) {\n return false;\n }\n for (i = 0; i < valArr.length; i++) {\n // Repopulate the old array\n const value = valArr[i];\n if (typeof value === 'undefined') {\n continue;\n }\n populateArr[i] = value;\n }\n return sortByReference || populateArr;\n}" const __locutus_standalone_ts_code = "function isPhpList(value) {\n return Array.isArray(value);\n}\nfunction isObjectLike(value) {\n return typeof value === 'object' && value !== null;\n}\nfunction isPhpAssocObject(value) {\n return isObjectLike(value) && !isPhpList(value);\n}\nfunction isPhpCallable(value) {\n return typeof value === 'function';\n}\nconst isIniBag = (value) => isPhpAssocObject(value);\nconst isLocaleBag = (value) => isPhpAssocObject(value);\nconst isLocaleCategoryBag = (value) => isPhpAssocObject(value);\nconst globalContext = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {};\nconst ensurePhpRuntimeObject = () => {\n let locutus = globalContext.$locutus;\n if (typeof locutus !== 'object' || locutus === null) {\n locutus = {};\n globalContext.$locutus = locutus;\n }\n let php = locutus.php;\n if (typeof php !== 'object' || php === null) {\n php = {};\n locutus.php = php;\n }\n return php;\n};\nfunction ensurePhpRuntimeState() {\n const php = ensurePhpRuntimeObject();\n const iniValue = php.ini;\n const localesValue = php.locales;\n const localeCategoriesValue = php.localeCategories;\n const pointersValue = php.pointers;\n const ini = isIniBag(iniValue) ? iniValue : {};\n const locales = isLocaleBag(localesValue) ? localesValue : {};\n const localeCategories = isLocaleCategoryBag(localeCategoriesValue) ? localeCategoriesValue : {};\n const pointers = Array.isArray(pointersValue) ? pointersValue : [];\n if (iniValue !== ini) {\n php.ini = ini;\n }\n if (localesValue !== locales) {\n php.locales = locales;\n }\n if (localeCategoriesValue !== localeCategories) {\n php.localeCategories = localeCategories;\n }\n if (pointersValue !== pointers) {\n php.pointers = pointers;\n }\n const localeDefaultValue = php.locale_default;\n const localeDefault = typeof localeDefaultValue === 'string' ? localeDefaultValue : undefined;\n return {\n ini,\n locales,\n localeCategories,\n pointers,\n locale_default: localeDefault,\n };\n}\nfunction getPhpGlobalEntry(key) {\n const value = globalContext[key];\n return typeof value === 'undefined' ? undefined : value;\n}\nfunction getPhpObjectEntry(value, key) {\n if ((typeof value !== 'object' && typeof value !== 'function') || value === null) {\n return undefined;\n }\n let current = value;\n while (current) {\n const descriptor = Object.getOwnPropertyDescriptor(current, key);\n if (descriptor) {\n if (typeof descriptor.get === 'function') {\n const getterValue = descriptor.get.call(value);\n return typeof getterValue === 'undefined' ? undefined : getterValue;\n }\n const directValue = descriptor.value;\n return typeof directValue === 'undefined' ? undefined : directValue;\n }\n current = Object.getPrototypeOf(current);\n }\n return undefined;\n}\nfunction resolvePhpCallable(callback, options) {\n if (isPhpCallable(callback)) {\n return { fn: callback, scope: null };\n }\n if (typeof callback === 'string') {\n const candidate = getPhpGlobalEntry(callback);\n if (isPhpCallable(candidate)) {\n return { fn: candidate, scope: null };\n }\n throw new Error(options.invalidMessage);\n }\n if (Array.isArray(callback) && callback.length >= 2) {\n const scopeDescriptor = callback[0];\n const callableDescriptor = callback[1];\n let scope;\n if (typeof scopeDescriptor === 'string') {\n scope = getPhpGlobalEntry(scopeDescriptor);\n if (typeof scope === 'undefined' && options.missingScopeMessage) {\n throw new Error(options.missingScopeMessage(scopeDescriptor));\n }\n }\n else {\n scope = scopeDescriptor;\n }\n if (isPhpCallable(callableDescriptor)) {\n return { fn: callableDescriptor, scope };\n }\n if (typeof callableDescriptor === 'string' && (isObjectLike(scope) || typeof scope === 'function')) {\n const candidate = getPhpObjectEntry(scope, callableDescriptor);\n if (isPhpCallable(candidate)) {\n return { fn: candidate, scope };\n }\n }\n }\n throw new Error(options.invalidMessage);\n}\nfunction usort(inputArr, sorter) {\n // discuss at: https://locutus.io/php/usort/\n // original by: Brett Zamir (https://brett-zamir.me)\n // improved by: Brett Zamir (https://brett-zamir.me)\n // note 1: This function deviates from PHP in returning a copy of the array instead\n // note 1: of acting by reference and returning true; this was necessary because\n // note 1: IE does not allow deleting and re-adding of properties without caching\n // note 1: of property position; you can set the ini of \"locutus.sortByReference\" to true to\n // note 1: get the PHP behavior, but use this only if you are in an environment\n // note 1: such as Firefox extensions where for-in iteration order is fixed and true\n // note 1: property deletion is supported. Note that we intend to implement the PHP\n // note 1: behavior by default if IE ever does allow it; only gives shallow copy since\n // note 1: is by reference in PHP anyways\n // example 1: var $stuff = {d: '3', a: '1', b: '11', c: '4'}\n // example 1: usort($stuff, function (a, b) { return (a - b) })\n // example 1: var $result = $stuff\n // returns 1: {0: '1', 1: '3', 2: '4', 3: '11'}\n const valArr = [];\n let k = '';\n let i = 0;\n let sortByReference = false;\n let populateArr = {};\n const normalizedSorter = typeof sorter === 'string' ? [this, sorter] : sorter;\n let comparator;\n try {\n const resolved = resolvePhpCallable(normalizedSorter, {\n invalidMessage: 'usort(): Invalid callback',\n missingScopeMessage: (scopeName) => 'Object not found: ' + scopeName,\n });\n comparator = (a, b) => Number(resolved.fn.apply(resolved.scope, [a, b]));\n }\n catch (_error) {\n return false;\n }\n const runtime = ensurePhpRuntimeState();\n const iniVal = String(runtime.ini['locutus.sortByReference']?.local_value ?? '') || 'on';\n sortByReference = iniVal === 'on';\n populateArr = sortByReference ? inputArr : populateArr;\n for (k in inputArr) {\n // Get key and value arrays\n if (inputArr.hasOwnProperty(k)) {\n const value = inputArr[k];\n if (typeof value !== 'undefined') {\n valArr.push(value);\n }\n if (sortByReference) {\n delete inputArr[k];\n }\n }\n }\n try {\n if (typeof comparator !== 'function') {\n return false;\n }\n valArr.sort(comparator);\n }\n catch (_e) {\n return false;\n }\n for (i = 0; i < valArr.length; i++) {\n // Repopulate the old array\n const value = valArr[i];\n if (typeof value === 'undefined') {\n continue;\n }\n populateArr[i] = value;\n }\n return sortByReference || populateArr;\n}" const __locutus_standalone_js_code = "// php/_helpers/_phpTypes (Locutus helper dependency)\nfunction isObjectLike(value) {\n return typeof value === 'object' && value !== null;\n}\nfunction isPhpAssocObject(value) {\n return isObjectLike(value) && !Array.isArray(value);\n}\nfunction isPhpCallable(value) {\n return typeof value === 'function';\n}\n// php/_helpers/_phpRuntimeState (Locutus helper dependency)\nconst globalContext = typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {};\nconst ensurePhpRuntimeObject = () => {\n let locutus = globalContext.$locutus;\n if (typeof locutus !== 'object' || locutus === null) {\n locutus = {};\n globalContext.$locutus = locutus;\n }\n let php = locutus.php;\n if (typeof php !== 'object' || php === null) {\n php = {};\n locutus.php = php;\n }\n return php;\n};\nfunction ensurePhpRuntimeState() {\n const php = ensurePhpRuntimeObject();\n const iniValue = php.ini;\n const localesValue = php.locales;\n const localeCategoriesValue = php.localeCategories;\n const pointersValue = php.pointers;\n const ini = isPhpAssocObject(iniValue) ? iniValue : {};\n const locales = isPhpAssocObject(localesValue) ? localesValue : {};\n const localeCategories = isPhpAssocObject(localeCategoriesValue) ? localeCategoriesValue : {};\n const pointers = Array.isArray(pointersValue) ? pointersValue : [];\n if (iniValue !== ini) {\n php.ini = ini;\n }\n if (localesValue !== locales) {\n php.locales = locales;\n }\n if (localeCategoriesValue !== localeCategories) {\n php.localeCategories = localeCategories;\n }\n if (pointersValue !== pointers) {\n php.pointers = pointers;\n }\n const localeDefaultValue = php.locale_default;\n const localeDefault = typeof localeDefaultValue === 'string' ? localeDefaultValue : undefined;\n return {\n ini,\n locales,\n localeCategories,\n pointers,\n locale_default: localeDefault,\n };\n}\nfunction getPhpGlobalEntry(key) {\n const value = globalContext[key];\n return typeof value === 'undefined' ? undefined : value;\n}\nfunction getPhpObjectEntry(value, key) {\n if ((typeof value !== 'object' && typeof value !== 'function') || value === null) {\n return undefined;\n }\n let current = value;\n while (current) {\n const descriptor = Object.getOwnPropertyDescriptor(current, key);\n if (descriptor) {\n if (typeof descriptor.get === 'function') {\n const getterValue = descriptor.get.call(value);\n return typeof getterValue === 'undefined' ? undefined : getterValue;\n }\n const directValue = descriptor.value;\n return typeof directValue === 'undefined' ? undefined : directValue;\n }\n current = Object.getPrototypeOf(current);\n }\n return undefined;\n}\n// php/_helpers/_callbackResolver (Locutus helper dependency)\nfunction resolvePhpCallable(callback, options) {\n if (isPhpCallable(callback)) {\n return { fn: callback, scope: null };\n }\n if (typeof callback === 'string') {\n const candidate = getPhpGlobalEntry(callback);\n if (isPhpCallable(candidate)) {\n return { fn: candidate, scope: null };\n }\n throw new Error(options.invalidMessage);\n }\n if (Array.isArray(callback) && callback.length >= 2) {\n const scopeDescriptor = callback[0];\n const callableDescriptor = callback[1];\n let scope;\n if (typeof scopeDescriptor === 'string') {\n scope = getPhpGlobalEntry(scopeDescriptor);\n if (typeof scope === 'undefined' && options.missingScopeMessage) {\n throw new Error(options.missingScopeMessage(scopeDescriptor));\n }\n }\n else {\n scope = scopeDescriptor;\n }\n if (isPhpCallable(callableDescriptor)) {\n return { fn: callableDescriptor, scope };\n }\n if (typeof callableDescriptor === 'string' && (isObjectLike(scope) || typeof scope === 'function')) {\n const candidate = getPhpObjectEntry(scope, callableDescriptor);\n if (isPhpCallable(candidate)) {\n return { fn: candidate, scope };\n }\n }\n }\n throw new Error(options.invalidMessage);\n}\n// php/array/usort (target function module)\nfunction usort(inputArr, sorter) {\n // discuss at: https://locutus.io/php/usort/\n // original by: Brett Zamir (https://brett-zamir.me)\n // improved by: Brett Zamir (https://brett-zamir.me)\n // note 1: This function deviates from PHP in returning a copy of the array instead\n // note 1: of acting by reference and returning true; this was necessary because\n // note 1: IE does not allow deleting and re-adding of properties without caching\n // note 1: of property position; you can set the ini of \"locutus.sortByReference\" to true to\n // note 1: get the PHP behavior, but use this only if you are in an environment\n // note 1: such as Firefox extensions where for-in iteration order is fixed and true\n // note 1: property deletion is supported. Note that we intend to implement the PHP\n // note 1: behavior by default if IE ever does allow it; only gives shallow copy since\n // note 1: is by reference in PHP anyways\n // example 1: var $stuff = {d: '3', a: '1', b: '11', c: '4'}\n // example 1: usort($stuff, function (a, b) { return (a - b) })\n // example 1: var $result = $stuff\n // returns 1: {0: '1', 1: '3', 2: '4', 3: '11'}\n const valArr = [];\n let k = '';\n let i = 0;\n let sortByReference = false;\n let populateArr = {};\n const normalizedSorter = typeof sorter === 'string' ? [this, sorter] : sorter;\n let comparator;\n try {\n const resolved = resolvePhpCallable(normalizedSorter, {\n invalidMessage: 'usort(): Invalid callback',\n missingScopeMessage: (scopeName) => 'Object not found: ' + scopeName,\n });\n comparator = (a, b) => Number(resolved.fn.apply(resolved.scope, [a, b]));\n }\n catch (_error) {\n return false;\n }\n const runtime = ensurePhpRuntimeState();\n const iniVal = String(runtime.ini['locutus.sortByReference']?.local_value ?? '') || 'on';\n sortByReference = iniVal === 'on';\n populateArr = sortByReference ? inputArr : populateArr;\n for (k in inputArr) {\n // Get key and value arrays\n if (inputArr.hasOwnProperty(k)) {\n const value = inputArr[k];\n if (typeof value !== 'undefined') {\n valArr.push(value);\n }\n if (sortByReference) {\n delete inputArr[k];\n }\n }\n }\n try {\n if (typeof comparator !== 'function') {\n return false;\n }\n valArr.sort(comparator);\n }\n catch (_e) {\n return false;\n }\n for (i = 0; i < valArr.length; i++) {\n // Repopulate the old array\n const value = valArr[i];\n if (typeof value === 'undefined') {\n continue;\n }\n populateArr[i] = value;\n }\n return sortByReference || populateArr;\n}" const __locutus_eval_function = (compiledCode: string): ((...args: unknown[]) => unknown) => { const evaluator = new Function('require', compiledCode + '\nreturn ' + __locutus_func_name + ';') return evaluator(__locutus_source_require) as (...args: unknown[]) => unknown } const __locutus_eval_module_export = (compiledCode: string, exportName: string): ((...args: unknown[]) => unknown) => { const module = { exports: {} as { [key: string]: unknown } } const exports = module.exports const evaluator = new Function('exports', 'module', 'require', compiledCode) evaluator(exports, module, __locutus_source_require) return module.exports[exportName] as (...args: unknown[]) => unknown } const __locutus_module_js_fn = __locutus_eval_module_export(__locutus_module_js_code, __locutus_func_name) const __locutus_standalone_ts_fn = __locutus_eval_function(__locutus_standalone_ts_code) const __locutus_standalone_js_fn = __locutus_eval_function(__locutus_standalone_js_code) describe('src/php/array/usort.ts (tested in test/generated/php/array/usort.vitest.ts)', function () { it('should pass example 1', function () { const expected = {0: '1', 1: '3', 2: '4', 3: '11'} const __locutus_variants: Array<{ name: string; fn: (...args: unknown[]) => unknown }> = [ { name: "source", fn: __locutus_source_fn }, { name: "module-js", fn: __locutus_module_js_fn }, { name: "standalone-ts", fn: __locutus_standalone_ts_fn }, { name: "standalone-js", fn: __locutus_standalone_js_fn }, ] const __locutus_run_example = (usort: typeof __locutus_source_fn) => { var $stuff = {d: '3', a: '1', b: '11', c: '4'} usort($stuff, function (a, b) { return (a - b) }) return $stuff } for (const __locutus_variant of __locutus_variants) { ;(globalThis as { $locutus?: unknown }).$locutus = undefined const result = __locutus_run_example(__locutus_variant.fn as typeof __locutus_source_fn) expect(result).toEqual(expected) } }) })