Code Monkey home page Code Monkey logo

search-string's Introduction

Search String

Another simple parser for advanced search query syntax.

Build Status npm version

It parses typical Gmail-style search strings like:

to:me -from:[email protected] foobar1 -foobar2

And returns an instance of SearchString which can be mutated, return different data structures, or return the gmail-style search string again.

See this blog post introducing this library.

Installation

$ npm install search-string

Usage

const SearchString = require('search-string');

// Perform parsing
const str = 'to:me -from:[email protected] foobar1 -foobar2';
const searchString = SearchString.parse(str);

/* Get text in different formats. */

// [ { text: 'foorbar1', negated: false }, { text: 'foobar2', negated: true } ]
searchString.getTextSegments();

// `foobar1 -foobar2`
searchString.getAllText();


/* Get conditions in different formats. */

// Standard format: Condition Array
// [ { keyword: 'to', value: 'me', negated: false }, { keyword: 'from', value: '[email protected]', negated: true } ]
searchString.getConditionArray(); 

// Alternate format: Parsed Query
// { to: ['me'], excludes: { from: ['[email protected]'] }}
searchString.getParsedQuery(); 

/* Or get text and conditions back in string format. */

// `to:me -from:[email protected] foobar1 -foobar2`
searchString.toString();


/* Mutations exist as well for modifying an existing SearchString structure. */

// `to:me foobar -foobar2`
searchString.removeKeyword('from', true).toString()

// `to:me from:[email protected] foobar1 -foobar2`
searchString.addEntry('from', '[email protected]', false).toString();

// `from:[email protected] foobar1 -foobar2`
searchString.removeEntry('to', 'me', false).toString();

/* clone operation instantiates a new version by copying over data. */

// `from:[email protected] foobar1 -foobar2`
searchString.clone().toString();

Testing

Run tests with npm test

or run tests on any changes with npm run testWatch

Building

Build ES5 compatible code with npm run babel

or continually build and watch for changes with npm run babelWatch

Background

See this blogpost for why this library was built and continues to be supported.

Thanks to search-query-parser for inspiration.

License

The MIT License (MIT)

Copyright (c) 2017 Mixmax, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

search-string's People

Contributors

bradvogel avatar guilhermemj avatar jsalvata avatar laggingreflex avatar megantinleywilson avatar mericsson avatar renovate[bot] avatar skeggse avatar spencer-brown avatar ttacon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

search-string's Issues

Conditions after free text

this.string = `${conditionStr} ${this.getAllText()}`.trim();

I know it's a tiny thing, but I think it makes more sense to have conditions follow rather than precede the freetext. I'm happy to submit a PR but would you rather it was an option or a default? Would it be an argument to toString()?

No semantic parsing or deduplication

Currently, the library allows for things like cheese and -cheese to be inputted, and doesn't reason about the semantic meaning of having a property and its negation both present. There's also no duplicate checking.

If we were to add duplicate checking, would we want to perform that automatically in an opinionated way? Or should it be user-configured?

How do we want to handle user input vs programmatic input? What kind of errors can we surface to the calling context which can help make the UX feel right?

example of where this has caused issues in the past:
https://github.com/mixmaxhq/app/pull/7505/files#diff-d32a3e6004a682c216ff83320b43464dR234 (private to Mixmaxers)

Typescript types

It would be nice to have a @ types/search-string to include in Typescript projects.

Does addEntry work with toString?

When trying the mentioned functions with quotes, I am confused where I thought the mixed quotes would be preserved after parsing the stringified query. Is the following behavior intended? or I simply misused the functions?

var SearchString = require("search-string")
const str1 = "";
const searchString1 = SearchString.parse(str1);
searchString1.addEntry('from', '\'"\'"', false);
console.log(searchString1.getConditionArray()[0].value) // '\"'\"

const str2 = searchString1.toString();
const searchString2 = SearchString.parse(str2);
console.log(searchString2.getConditionArray()[0].value) // \"\"

It should flag "quoted text" as must-include

In Gmail/Google, a "quoted text" means that it must be included in the search.

getTextSegments() currently gets rid of any extra quotes "". While it's nice that it does that, it'd be cool if it could also mark a flag like mustInclude:true (just like it marks negated for hyphen -)

Whitelisting conditions

Great job on this library!

One thing: it seems important to support a whitelist of conditions. What if I wanted to search for text that had colons in it? Let's say a book is called "Foobar: The story of Steve", and a user tries to search for that phrase? You'll currently strip Foobar: out of the text.

Most query DSLs will have a specific set of conditions that are valid. search-query-parser shows their usage here. It would make sense to ignore anything else, imo.

I guess you'd need a breaking change that looked something like this: parse(string, { config })

Remove Keyword Method Issue

removeKeyword(keywordToRemove, negatedToRemove) {

Solution:
removeKeyword(keywordToRemove, negatedToRemove) { this.textSegments = this.textSegments.filter( ({ value, negated }) => keywordToRemove !== value || negatedToRemove !== negated ); this.isStringDirty = true; }

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (local>mixmaxhq/renovate-config)

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.