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

ember-cli-filter-by-query

v1.4.0

Published

Provides you with a computed property that filters an array by a given search query. The output is sorted by similarity to the query.

Downloads

49

Readme

Ember-cli-filter-by-query

Build Status Code Climate

This addon provides you with a computed property macro to filter an array of objects based on a given search query. Other related addons often export components that might not suit your needs, ember-cli-filter-by-query only exports the macro and the filtering function itself so you can do whatever you want with it. Since the filtered list will always be sorted based on similarity to the search query a popular usecase could be autocompletion.

Under the hood it uses sifter.js, which is most likely faster then any filter solution you or I could come up with ;)

Requirements

  • As of version 1.4.0 requires Ember 3.24 or higher (4.x included). For older Ember versions use version 1.3.2 or lower.

Example

import computedFilterByQuery from 'ember-cli-filter-by-query';

Guy = DS.Model.extend({
  filteredList: computedFilterByQuery('friends', 'name', 'query'),
});

filteredList will include all friends, whos names match the value of the query property - sorted by similarity to the search term. You can also pass an array of property keys as the second argument and both will be matched - ordered with preceding priority.

import computedFilterByQuery from 'ember-cli-filter-by-query';

Guy = DS.Model.extend({
  filteredList: computedFilterByQuery('friends', ['name', 'surname'], 'query'),
});

filteredList will recompute, whenever the value of guy.get('query') or guy.get('friends.@each.{name,surname}') changes. If you are in need of the underlying filter method and don't want to wrap that in a computed property macro, you can access it too:

import filterByQuery from 'ember-cli-filter-by-query/util/filter';

filterByQuery(guy.get('friends'), ['name', 'surname'], controller.get('query'));

Notice that in this case, the first and last argument can't be property keys anymore but have to be the actual array and query.

additional Options

It is possible to pass a set of different options to the computed property macro aswell as to the utility function.

| Option | Type | Description | | ----------- | :------ | :--------------------------------------------------------------------------------------------------------------------- | | filter | boolean | If false, items with a score of zero will not be filtered out of the result-set. | | conjunction | string | Determines how multiple search terms are joined ("and" or "or"). | | sort | boolean | Default is true. If true, the output is sorted by score. If false, the output is in the same order as the input. |

import computedFilterByQuery from 'ember-cli-filter-by-query';

Guy = DS.Model.extend({

  smallList: computedFilterByQuery( 'friends', ['name', 'surname'], 'query', {conjunction: 'and' })
  // this will only list friends whos name or surname include every word in the query

  largeList: computedFilterByQuery( 'friends', ['name', 'surname'], 'query', {conjunction: 'or' })
  // this will list friends whos name or surname include at least one word of the query

});

Installation

To use this addon in your project, just type:

$ ember install ember-cli-filter-by-query

or for older versions of ember-cli (pre 1.4.0):

$ npm install --save-dev ember-cli-filter-by-query
$ ember generate ember-cli-filter-by-query

and then import the function wherever you need it:

import computedFilterByQuery from 'ember-cli-filter-by-query';

or

import filterByQuery from 'ember-cli-filter-by-query/util/filter';

Contributing

I am happy about any contributions or PRs. If you are missing some piece of functionality please open an issue. This addon is quite simple and can be extended easily. It is using sifter.js internally which has a richer API than what i am exposing here.

  • git clone https://github.com/lazybensch/ember-cli-filter-by-query
  • cd ember-cli-filter-by-query
  • npm install
  • bower install // As of ember-cli-filter-by-query 1.3.0 this step is not needed.
  • ember test

Changelog

  • 1.4.0
    • Updated to support Ember 4.x