Skip to content

Instantly share code, notes, and snippets.

@DaveyJake
Last active May 14, 2023 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveyJake/f8c5cc55a3531be72059ff28bf119092 to your computer and use it in GitHub Desktop.
Save DaveyJake/f8c5cc55a3531be72059ff28bf119092 to your computer and use it in GitHub Desktop.
PHP functions for Typescript
/**
* The following Typescript functions are inspired by {@link https://www.php.net/ PHP}.
*
* @author Davey Jacobson <daveyjake21 [at] geemail [dot] com>
* @version 1.0.1
*/
import each from 'lodash/each';
import fromPairs from 'lodash/fromPairs';
import includes from 'lodash/includes';
import isEqual from 'lodash/isEqual';
import isError from 'lodash/isError';
import isMap from 'lodash/isMap';
import isNaN from 'lodash/isNan';
import isNull from 'lodash/isNull';
import isSet from 'lodash/isSet';
import isUndefined from 'lodash/isUndefined';
import map from 'lodash/map';
import sortBy from 'lodash/sortBy';
import sum from 'lodash/sum';
import toNumber from 'lodash/toNumber';
/**
* PHP's {@link https://www.php.net/manual/en/function.empty.php empty} function,
* powered by Lodash and written for Typescript.
*
* @since 1.0.0
* @since 1.0.1 Added checks for `Map` and `Set`.
*
* @param value The value to check.
*
* @returns True if `value` is one of the conditions. False if not.
*/
function empty( value: any ): boolean {
// Check if `value` is a `Map` or `Set`.
if ( isMap( value ) || isSet( value ) ) {
value = value.size;
}
/**
* Conditions we want to return as true.
*
* @since 1.0.0
* @since 1.0.1 Added check for error objects and zeros with a decimal.
*/
let conditions: Array<any> = [
isEqual( value, 0 ),
isEqual( value, 0.0 ),
isEqual( value, '0' ),
isEqual( value, '0.0' ),
isEqual( value, false ),
isEqual( value, 'false' ),
isEqual( value, '' ),
isEqual( value, [] ),
isEqual( value, {} ),
isEqual( isError( value ), true ),
isEqual( isNaN( value ), true ),
isEqual( value, 'NaN' ),
isEqual( isNull( value ), true ),
isEqual( value, 'null' ),
isEqual( isUndefined( value ), true ),
isEqual( value, 'undefined' )
];
// Convert boolean results to integers: true = 1; false = 0.
conditions = map( conditions, toNumber );
// Get the sum of the results.
const result = sum( conditions );
// If the result is greater than 0, we know it's empty.
return result > 0;
}
/**
* Typescript version of PHP's {@link https://www.php.net/manual/en/function.in_array.php in_array} function.
*
* @since 1.0.0
*
* @example inArray( 'van', ['Ronnie', 'van', 'Zant'], true ); // returns true
*
* @param needle What to search for.
* @param haystack What to search through.
* @param argStrict Strict checking.
*
* @returns True if successful. False if not.
*/
function inArray( needle: any, haystack: any[], argStrict: boolean ): boolean {
const strict = !!argStrict; // eslint-disable-line
/*
* We prevent the double check (strict && arr[key] === ndl) || (!strict && arr[key] === ndl)
* in just one for, in order to improve the performance deciding which type
* of comparation will do before walking the array.
*/
if ( strict ) {
for ( const key in haystack ) {
if ( haystack[ key ] === needle ) {
return true;
}
}
}
return includes( haystack, needle );
}
/**
* Typescript version of PHP's {@link https://www.php.net/manual/en/function.ksort.php ksort}, powered by Lodash.
*
* @since 1.0.0
*
* @param object Object ot sort by keys.
*
* @returns Object sorted by keys.
*/
function ksort( object: any ): object {
const keys = Object.keys( object ),
sortedKeys = sortBy( keys );
return fromPairs( map( sortedKeys, key => [ key, object[ key ] ] ) );
}
/**
* Typescript version of PHP's {@link https://www.php.net/manual/en/function.ucfirst.php ucfirst}.
*
* @since 1.0.0
*
* @see empty()
*
* @param s Text to capitalize.
*
* @returns The capitalized string.
*/
function ucfirst( s: string ): string {
if ( empty( s.charAt( 0 ).match( /[a-z]/ ) ) ) {
return s.charAt( 1 ).toUpperCase() + s.slice( 2 );
}
return s.charAt( 0 ).toUpperCase() + s.slice( 1 );
}
/**
* Typescript version of PHP's {@link https://www.php.net/manual/en/function.ucwords.php ucwords}, powered by Lodash.
*
* @since 1.0.0
*
* @see ucfirst()
*
* @param s Text to capitalize.
*
* @returns Capitalized string.
*/
function ucwords( s: string ): string {
return map( s.split( /(\s|-)/ ), ucfirst ).join( ' ' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment