npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

teo

v0.3.0

Published

Functions to test/check, filter, find and process/transform objects

Downloads

8

Readme

teo

Functions to test/check, filter, find and process/transform objects.

NPM version Build Status Built with Grunt

Installation

Node

npm install teo

Component

component install gamtiq/teo

Jam

jam install teo

Bower

bower install teo

JSPM

jspm install teo

SPM

spm install teo

AMD, <script>

Use dist/teo.js or dist/teo.min.js (minified version).

Usage

Node, Component, JSPM, SPM

var teo = require("teo");

Duo

var teo = require("gamtiq/teo");
...

Jam

require(["teo"], function(teo) {
    ...
});

JSPM

System.import("teo").then(function(teo) {
    ...
});

AMD

define(["path/to/dist/teo.js"], function(teo) {
    ...
});

Bower, <script>


<!-- Use bower_components/teo/dist/teo.js if the library was installed by Bower -->

<script type="text/javascript" src="path/to/dist/teo.js"></script>
<script type="text/javascript">
    // teo is available via teo field of window object
    ...
</script>

Examples

teo.isObject(teo);   // true
teo.isObject(null);   // false
teo.isObject([]);   // false

teo.isEmpty(teo);   // false
teo.isEmpty([], true);   // true

var obj = {};
obj[Symbol("a")] = null;
teo.isEmpty(obj);   // false
teo.isEmpty(obj, true);   // true

teo.test({}, "true");   // true
teo.test({}, "empty");   // true
teo.test([], "empty");   // true
teo.test(teo, "empty");   // false
teo.test([0], "empty");   // false

teo.test({}, {});   // true
teo.test({a: 1}, {a: 2});   // false
teo.test({a: 1, b: 2, c: 3, d: 4}, {c: 3, a: 1});   // true

teo.test(1, teo.isObject);   // false
teo.test("", false);   // true

var personList = [
    {name: "Adam", age: 27, married: true, children: 1},
    {name: "Eva", age: 23, married: true, children: 1},
    {name: "Carl", age: 59, married: true, children: 3},
    {name: "Daniel", age: 17, married: false, children: 0},
    {name: "Gloria", age: 28, married: false, children: 1},
    {name: "Viola", age: 35, married: true, children: 4},
    {name: "Leonardo", age: 61, married: false, children: 1},
    {name: "Patricia", age: 44, married: false, children: 2}
];

teo.filterList([0, "", true, "ret"], "true");   // [true, "ret"]
teo.filterList([3, -4, 2, 10, 7, -9, 5], function(n) {return n >= 3;}, {count: true});   // 4
teo.filterList(personList,
                function(person) {return person.age > 30;},
                {transform: function(person) {return person.name;}});   // ["Carl", "Viola", "Leonardo", "Patricia"]
                            
teo.findItemIndex(personList, {married: false, children: 1});   // 4
teo.findItem(personList, function(person) {return person.age > 30 && ! person.married;});   // {name: "Leonardo", age: 61, married: false, children: 1}

teo.map({a: 1, b: 2, c: null, d: "delta", e: null, f: undefined},
        function(context) {return false;},
        {filter: {value: null}});          // {a: 1, b: 2, c: false, d: "delta", e: false, f: undefined}

teo.map({a5: 1, b3: "center", c5: null, d: "delta", e8: -5, field9: 99, g99: -38},
        null,
        {
            rename: function(context) {
                var sField = context.field,
                    match = sField.match(/^\w\d+$/);
                return typeof context.value === "number" && match
                            ? "n_" + match[0]
                            : sField;
            }
        });   // {n_a5: 1, b3: "center", c5: null, d: "delta", n_e8: -5, field9: 99, n_g99: -38}


function convert(context) {
    var value = context.value,
        bNoValue = value == null,
        sType = typeof value;
    if (context.test) {
        return bNoValue || sType === "object" || (sType === "string" && /^-?\d+$/.test(value));
    }
    else {
        return bNoValue
                ? 0
                : Number(value);
    }
}

teo.map({
            a: "abc",
            b: "25",
            c: {
                d: null,
                e: "eclipse",
                f: {
                    g: undefined,
                    h: "-59",
                    i: "JS 2015"
                }
            }
        },
        convert,
        {filter: convert, recursion: true});
// returns
// {
//      a: "abc",
//      b: 25,
//      c: {
//          d: 0,
//          e: "eclipse",
//          f: {
//              g: 0,
//              h: -59,
//              i: "JS 2015"
//          }
//      }
//  }

See tests for additional examples.

API

isObject(value): Boolean

Check whether value is real object (not array nor function).

isEmpty(obj: Object, [ignoreSymbolFields: Boolean]): Boolean

Check whether object do not contain any fields.

test(obj: Object, filter: Object | Function | Array | String): Boolean

Check whether object conforms to specified condition/filter.

filterList(list: Array, filter: Object | Function | Array | String, [settings: Object]): Array | Integer

Form new array containing elements from the source array which conform to the given condition (filter) or calculate quantity of such elements.

findItemIndex(list: Array, filter: Object | Function | Array | String): Integer

Return the index of the first element in the array that conforms to the given condition (filter).

findItem(list: Array, filter: Object | Function | Array | String): Any

Return the first element in the array that conforms to the given condition (filter).

map(source: Object, action: Function | Object, [settings: Object]): Object

Execute the specified action for fields of the object and return the object containing results of processing.

See doc folder for details.

Related projects

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

Copyright (c) 2014-2015 Denis Sikuler
Licensed under the MIT license.