Skip to content

Instantly share code, notes, and snippets.

@DaveyJake
Last active May 14, 2023 10:49
Show Gist options
  • Save DaveyJake/f8c5cc55a3531be72059ff28bf119092 to your computer and use it in GitHub Desktop.
Save DaveyJake/f8c5cc55a3531be72059ff28bf119092 to your computer and use it in GitHub Desktop.

Revisions

  1. DaveyJake revised this gist May 14, 2023. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion php.ts
    Original file line number Diff line number Diff line change
    @@ -20,7 +20,8 @@ import sum from 'lodash/sum';
    import toNumber from 'lodash/toNumber';

    /**
    * Typescript version of PHP's [`empty`](https://www.php.net/manual/en/function.empty.php) function, powered by Lodash.
    * 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`.
  2. DaveyJake revised this gist May 14, 2023. 1 changed file with 16 additions and 7 deletions.
    23 changes: 16 additions & 7 deletions php.ts
    Original file line number Diff line number Diff line change
    @@ -2,43 +2,56 @@
    * The following Typescript functions are inspired by {@link https://www.php.net/ PHP}.
    *
    * @author Davey Jacobson <daveyjake21 [at] geemail [dot] com>
    * @version 1.0.0
    * @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';

    /**
    * Typescript version of PHP's {@link https://www.php.net/manual/en/function.empty.php empty} function, powered by Lodash.
    * Typescript version of PHP's [`empty`](https://www.php.net/manual/en/function.empty.php) function, powered by Lodash.
    *
    * @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 ),
    @@ -54,11 +67,7 @@ function empty( value: any ): boolean {
    const result = sum( conditions );

    // If the result is greater than 0, we know it's empty.
    if ( result > 0 ) {
    return true;
    }

    return false;
    return result > 0;
    }

    /**
  3. DaveyJake revised this gist May 11, 2023. No changes.
  4. DaveyJake revised this gist May 11, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion php.ts
    Original file line number Diff line number Diff line change
    @@ -140,5 +140,5 @@ function ucfirst( s: string ): string {
    * @returns Capitalized string.
    */
    function ucwords( s: string ): string {
    return map( s.split( /\s/ ), ucfirst ).join( ' ' );
    return map( s.split( /(\s|-)/ ), ucfirst ).join( ' ' );
    }
  5. DaveyJake created this gist May 11, 2023.
    144 changes: 144 additions & 0 deletions php.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,144 @@
    /**
    * The following Typescript functions are inspired by {@link https://www.php.net/ PHP}.
    *
    * @author Davey Jacobson <daveyjake21 [at] geemail [dot] com>
    * @version 1.0.0
    */
    import each from 'lodash/each';
    import fromPairs from 'lodash/fromPairs';
    import includes from 'lodash/includes';
    import isEqual from 'lodash/isEqual';
    import isNaN from 'lodash/isNan';
    import isNull from 'lodash/isNull';
    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';

    /**
    * Typescript version of PHP's {@link https://www.php.net/manual/en/function.empty.php empty} function, powered by Lodash.
    *
    * @since 1.0.0
    *
    * @param value The value to check.
    *
    * @returns True if `value` is one of the conditions. False if not.
    */
    function empty( value: any ): boolean {
    /**
    * Conditions we want to return as true.
    *
    * @since 1.0.0
    */
    let conditions: Array<any> = [
    isEqual( value, 0 ),
    isEqual( value, '0' ),
    isEqual( value, false ),
    isEqual( value, 'false' ),
    isEqual( value, '' ),
    isEqual( value, [] ),
    isEqual( value, {} ),
    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.
    if ( result > 0 ) {
    return true;
    }

    return false;
    }

    /**
    * 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( ' ' );
    }