---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- sizeof([[0,0],[0,-4]], 'COUNT_RECURSIVE')
- 'sizeof({''one'' : [1,2,3,4,5]}, ''COUNT_RECURSIVE'')'
returns:
- '6'
- '6'
dependencies:
- php/array/count
authors:
original by:
- Philip Peterson
notes: []
parityVerified: null
isTypescript: true
type: function
layout: function
title: PHP's sizeof in TypeScript
description: >-
Here's what our current TypeScript equivalent to PHP's sizeof looks
like.
function: sizeof
category: array
language: php
permalink: php/array/sizeof/
alias:
- /functions/php/sizeof/
- /functions/array/sizeof/
- /php/sizeof/
- /functions/sizeof/
---
{% codeblock lang:typescript %}import { type Countable, type CountMode, count } from '../array/count.ts'
export function sizeof(mixedVar: Countable, mode?: CountMode): number {
// discuss at: https://locutus.io/php/sizeof/
// original by: Philip Peterson
// example 1: sizeof([[0,0],[0,-4]], 'COUNT_RECURSIVE')
// returns 1: 6
// example 2: sizeof({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE')
// returns 2: 6
return count(mixedVar, mode ?? 0)
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { count } from '../array/count.ts'
export function sizeof(mixedVar, mode) {
// discuss at: https://locutus.io/php/sizeof/
// original by: Philip Peterson
// example 1: sizeof([[0,0],[0,-4]], 'COUNT_RECURSIVE')
// returns 1: 6
// example 2: sizeof({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE')
// returns 2: 6
return count(mixedVar, mode ?? 0)
}{% endcodeblock %}
{% codeblock lang:typescript %}// php/_helpers/_phpTypes (Locutus helper dependency)
type PhpNullish = null | undefined
type PhpInput = {} | PhpNullish
type PhpScalar = string | number | boolean
type PhpPrimitive = PhpScalar | bigint
type PhpFunctionValue = (...args: PhpInput[]) => PhpInput
interface PhpRuntimeAssoc {
[key: string]: PhpRuntimeValue
}
interface PhpRuntimeList extends Array
{}
type PhpRuntimeValue = PhpPrimitive | PhpNullish | PhpRuntimeList | PhpRuntimeAssoc | PhpFunctionValue
// php/array/count (Locutus dependency module)
type CountValue = PhpRuntimeValue
type CountableList = CountValue[]
interface CountableAssoc {
[key: string]: CountValue
}
type Countable = CountableList | CountableAssoc
type CountMode = 0 | 1 | 'COUNT_NORMAL' | 'COUNT_RECURSIVE'
const isCountable = (value: CountValue): value is Countable => {
if (!value || typeof value !== 'object') {
return false
}
const valuePrototype = Object.getPrototypeOf(value)
return valuePrototype === Array.prototype || valuePrototype === Object.prototype
}
function count(mixedVar: null | undefined, mode?: CountMode): 0
function count(mixedVar: Countable, mode?: CountMode): number
function count(mixedVar: Countable | null | undefined, mode: CountMode = 0): number {
// discuss at: https://locutus.io/php/count/
// original by: Kevin van Zonneveld (https://kvz.io)
// input by: Waldo Malqui Silva (https://waldo.malqui.info)
// input by: merabi
// bugfixed by: Soren Hansen
// bugfixed by: Olivier Louvignes (https://mg-crea.com/)
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: count([[0,0],[0,-4]], 'COUNT_RECURSIVE')
// returns 1: 6
// example 2: count({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE')
// returns 2: 6
let cnt = 0
if (mixedVar === null || typeof mixedVar === 'undefined') {
return 0
}
if (typeof mixedVar !== 'object') {
return 1
}
const prototype = Object.getPrototypeOf(mixedVar)
if (prototype !== Array.prototype && prototype !== Object.prototype) {
return 1
}
const recursiveMode = mode === 'COUNT_RECURSIVE' || mode === 1
if (Array.isArray(mixedVar)) {
for (const key of Object.keys(mixedVar)) {
cnt++
const value = mixedVar[Number(key)]
if (recursiveMode && isCountable(value)) {
cnt += count(value, 1)
}
}
return cnt
}
for (const value of Object.values(mixedVar)) {
cnt++
if (recursiveMode && isCountable(value)) {
cnt += count(value, 1)
}
}
return cnt
}
// php/array/sizeof (target function module)
function sizeof(mixedVar: Countable, mode?: CountMode): number {
// discuss at: https://locutus.io/php/sizeof/
// original by: Philip Peterson
// example 1: sizeof([[0,0],[0,-4]], 'COUNT_RECURSIVE')
// returns 1: 6
// example 2: sizeof({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE')
// returns 2: 6
return count(mixedVar, mode ?? 0)
}{% endcodeblock %}
{% codeblock lang:javascript %}// php/array/count (Locutus dependency module)
const isCountable = (value) => {
if (!value || typeof value !== 'object') {
return false
}
const valuePrototype = Object.getPrototypeOf(value)
return valuePrototype === Array.prototype || valuePrototype === Object.prototype
}
function count(mixedVar, mode = 0) {
// discuss at: https://locutus.io/php/count/
// original by: Kevin van Zonneveld (https://kvz.io)
// input by: Waldo Malqui Silva (https://waldo.malqui.info)
// input by: merabi
// bugfixed by: Soren Hansen
// bugfixed by: Olivier Louvignes (https://mg-crea.com/)
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: count([[0,0],[0,-4]], 'COUNT_RECURSIVE')
// returns 1: 6
// example 2: count({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE')
// returns 2: 6
let cnt = 0
if (mixedVar === null || typeof mixedVar === 'undefined') {
return 0
}
if (typeof mixedVar !== 'object') {
return 1
}
const prototype = Object.getPrototypeOf(mixedVar)
if (prototype !== Array.prototype && prototype !== Object.prototype) {
return 1
}
const recursiveMode = mode === 'COUNT_RECURSIVE' || mode === 1
if (Array.isArray(mixedVar)) {
for (const key of Object.keys(mixedVar)) {
cnt++
const value = mixedVar[Number(key)]
if (recursiveMode && isCountable(value)) {
cnt += count(value, 1)
}
}
return cnt
}
for (const value of Object.values(mixedVar)) {
cnt++
if (recursiveMode && isCountable(value)) {
cnt += count(value, 1)
}
}
return cnt
}
// php/array/sizeof (target function module)
function sizeof(mixedVar, mode) {
// discuss at: https://locutus.io/php/sizeof/
// original by: Philip Peterson
// example 1: sizeof([[0,0],[0,-4]], 'COUNT_RECURSIVE')
// returns 1: 6
// example 2: sizeof({'one' : [1,2,3,4,5]}, 'COUNT_RECURSIVE')
// returns 2: 6
return count(mixedVar, mode ?? 0)
}{% endcodeblock %}