GithubHelp home page GithubHelp logo

isabella232 / traverse-reshape Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alleyinteractive/traverse-reshape

0.0 0.0 0.0 24 KB

Safely break down arrays or objects, and put them back together in new shapes.

License: GNU General Public License v2.0

PHP 100.00%

traverse-reshape's Introduction

traverse/reshape

traverse() and reshape() are companion functions that safely break down arrays or objects and put them back together in new shapes.

traverse

Traverse an array or an object using a delimiter to find one value or many values.

<?php

$arr = [
    'apples' => [
        'red' => [
            'gala',
            'mcintosh',
        ],
        'green' => [
            'granny_smith',
        ],
    ],
];

$obj = (object) [
    'apples' => (object) [
        'red' => [
            'gala',
            'mcintosh',
        ],
        'green' => [
            'granny_smith',
        ],
    ],
];


$green = \Alley\traverse($arr, 'apples.green');
// ['granny_smith']

$red = \Alley\traverse($obj, 'apples.red');
// ['gala', 'mcintosh']

[$red, $green] = \Alley\traverse($obj, ['apples.red', 'apples.green']);
// ['gala', 'mcintosh'], ['granny_smith']

[[$red, $green]] = \Alley\traverse(
    $obj,
    [
        'apples' => [
            'red',
            'green',
       ],
   ],
);
// ['gala', 'mcintosh'], ['granny_smith']
// note the extra depth of the return value -- values are nested according to the nesting of the given paths

[$red] = \Alley\traverse($obj, ['apples' => 'red']);
// ['gala', 'mcintosh']

$sweet = \Alley\traverse($arr, 'apples.green.sweet');
// NULL

$pears = \Alley\traverse($arr, 'pears');
// NULL

$req = getRemoteData();
[$title, $date] = \Alley\traverse($req, ['title', 'date']);
// $title and $date variables are guaranteed defined regardless of $req

[[$red, $green], $title] = \Alley\traverse(
    [$arr, $req],
    [
        '0.apples' => ['red', 'green'],
        '1.title',
   ]
);

reshape

Declare the shape of a new array or object whose values are extracted from a source array or object with traverse().

Shapes can be multidimensional. Paths that do not resolve in the source will be null in the result. If a path is given without a key, the key will be inferred from the path. Passing an object for a shape returns an object instead of an array.

<?php

$original = [
    'id' => 1,
    'title' => [
        'rendered' => 'Hello world!',
    ],
    'content' => [
        'rendered' => '<p>Welcome to WordPress. This is your first post. Edit or delete it, then start writing!</p>',
    ],
    'categories' => [1],
    'tags' => [],
    '_links' => [
        'self' => [
            [
                'href' => 'https://www.example.com/wp-json/wp/v2/posts/1',
            ],
        ],
    ],
];

\Alley\reshape(
    $original,
    [
        'title' => 'title.rendered',
        'dek' => 'meta.dek',
        'content.rendered',
        'term_ids' => (object) [
            'category' => 'categories',
            'post_tag' => 'tags',
        ],
        'json' => '_links.self.0.href',
    ]
);

/*
    [
        'title' => 'Hello world!',
        'dek' => NULL,
        'rendered' => '<p>Welcome to WordPress...',
        'term_ids' => (object) [
            'category' => [1],
            'post_tag' => [],
        ],
        'json' => 'https://www.example.com/...',
    ]
*/

traverse-reshape's People

Contributors

dlh01 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.