Giter Site home page Giter Site logo

nlinq's Introduction

nLinq

Build

Aims to be a replacement to the old jLinq providing most of the functionality hopefully faster than the original.

The aim is to provide a declarative JavaScript abstraction over different ways of "querying" arrays of maps/objects.

Examples:

// How many contacts there are with email ending in @domain.com
$from( arrayOfContacts )
.ends("email", "@domain.com")
.count();

// Sort all contacts in @domain.com by first name and last name
$from( arrayOfContacts )
.ends("email", "@domain.com")
.sort("firstName", "lastName")
.select()

Althought some code (OpenAFSigil) is borrowed from OpenAF it will be released for both in browser use and in OpenAF use.

Check out the nLinq reference.

Using in a browser

Example of using nLinq in a browser:

<html>
 <body>
  <h1>Test nLinq</h1>
  <script src="dist/nlinq.min.js"></script>
  <hr>

  <script> 
    var family = [
      { id: 1, firstName: "John", lastName: "Smith", bornYear: 1982, state: "NY" },
      { id: 2, firstName: "Anne", lastName: "Smith", bornYear: 1985, state: "NY" },
      { id: 3, firstName: "Steve", lastName: "Andrews", bornYear: 1997, state: "CA" },
      { id: 4, firstName: "Greg", lastName: "Andrews", bornYear: 1995, state: "CA" },
      { id: 5, firstName: "Louise", lastName: "Andrews", bornYear: 2012, state: "NY" },
      { id: 6, firstName: "Paul", "lastName": "Smith", bornYear: 2020, state: "CA" }
    ] 

    var _w = r => document.write(r)
    var showName = r => r.firstName + " " + r.lastName
    var _wArr = r => _w(r.map(showName).join(", ")) 
  </script>

  <p><b>Smiths in CA: </b><script>_wArr( $from( family )
                                        .equals("lastName", "Smith")
                                        .equals("state", "CA")
                                        .select())</script></p>

  <p><b>Youngest: </b><script>_w(showName( $from( family )
                                           .attach("age", r => new Date().getFullYear() - r.bornYear)
                                           .min("age")))</script></p>

 </body>
</html>

Currently not implemented comparing jLinq

This is not an exaustive list by just to provide a heads-up on what is still missing:

Functionality

  • Parameter arrays to apply functions over values.
  • Parameter on or, and, not, andNot & orNot.
  • Context key aware "where" methods.

nlinq's People

Contributors

github-actions[bot] avatar nmaguiar avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

nlinq's Issues

Cannot set property "_key" of null

Problem

When using $from under a map with a null entry an error equivalent to:

TypeError: Cannot set property "_key" of null to "b"

occurs.

Steps to reproduce:

var o = { a: 1, b: null, c: 2 };
$from(o)

Convert fields to a date type

In several cases is useful to sort an array by date but the field might not be a date type. So you end up doing this:

$from(array_of_data)
.attach(“date2”, r => new Date(r.date))
.sort(“date2”)
.select(r => ({ filename: r.filename, date: r.date2 })

It would be more practical and simple just to do:

$from(array_of_data)
.toDate(“date”)
.sort(“date”)
.select({ filename: “n/a”, date: “” })

Add path-based key references

Be able to reference sub-maps and arrays using OpenAFSigil $$().get().

Example:

_from( anArray )
.equals("names.firstName", "Joe")
.select()

useCase incorrectly changing settings

Problem

By default nLinq will be case insensitive:

> $from(["hello"]).equals("HELLO").any()
> false

but when setting useCase to true:

> $from(["hello"]).useCase(true).equals("HELLO").any()
> true

the result should be false.

List of fields selector

It’s already possible to map which fields to will be returned has a result:

// Providing the defaults
$from(array_of_data).select({ field1: “n/a”, field2: 0 })

// OR

$from(array_of_data).select(r => ({ f1: r.field1, field2: r.field2 }))

But it could be more practical to have an array of keys to do the same as the last function option:

$from(array_of_data).select([ “f1:field1”, “field2” ])

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.