Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Typescript #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ $ npm install search-query-parser
## Usage

```javascript
var searchQuery = require('search-query-parser');
import * as SearchQueryParser from 'search-query-parser';

var query = 'from:[email protected],[email protected] to:me subject:vacations date:1/10/2013-15/04/2014 photos';
var options = {keywords: ['from', 'to', 'subject'], ranges: ['date']}
Expand All @@ -63,18 +63,18 @@ It accepts 5 values:
If no keywords or ranges are specified, or if none are present in the given search query, then `searchQuery.parse` will return a string if `tokenize` is false, or an array of strings under the key `text` if `tokenize` is true.

```javascript
var searchQuery = require('search-query-parser');
import * as SearchQueryParser from 'search-query-parser';

var query = 'a query with "just text"';
var parsedQuery = searchQuery.parse(query);
var parsedQuery = SearchQueryParser.parse(query);
// parsedQuery is now 'a query with "just text"'

var options = {keywords: ['unused']};
var parsedQueryWithOptions = searchQuery.parse(query, options);
var parsedQueryWithOptions = SearchQueryParser.parse(query, options);
// parsedQueryWithOptions is now 'a query with "just text"'

var options2 = {tokenize: true};
var parsedQueryWithTokens = searchQuery.parse(query, options2);
var parsedQueryWithTokens = SearchQueryParser.parse(query, options2);
// parsedQueryWithTokens is now: ['a', 'query', 'with', 'just text']
```

Expand All @@ -92,19 +92,19 @@ You can also use exclusion syntax, like `-from:[email protected] name:hello,world`.
Sometimes checking against whether a keyword holds string or not can be excessive and prone to errors; it's often easier to simply expect everything is an array even if it means doing 1-iteration loops often.

```javascript
var searchQuery = require('search-query-parser');
import * as SearchQueryParser from 'search-query-parser';

var query = 'test:helloworld fun:yay,happy';
var options = {keywords: ['test', 'fun']};
var parsedQueryWithOptions = searchQuery.parse(query, options);
var parsedQueryWithOptions = SearchQueryParser.parse(query, options);
// parsedQueryWithOptions is now:
// {
// test: 'helloworld',
// fun: ['yay', 'happy']
// }

var optionsAlwaysArray = {keywords: ['test', 'fun'], alwaysArray: true};
var parsedQueryWithOptions = searchQuery.parse(query, options);
var parsedQueryWithOptions = SearchQueryParser.parse(query, options);
// parsedQueryWithOptions is now:
// {
// test: ['helloworld'], //No need to check whether test is a string or not!
Expand Down
18 changes: 18 additions & 0 deletions config/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var path = require('path');
var _root = path.resolve(__dirname, '..');

function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [_root].concat(args));
}

exports.root = root;

function isVendor(module, count) {
if (typeof module.context !== 'string') {
return false;
}
return module.context.indexOf('/node_modules/') >= 0
}

exports.isVendor = isVendor;
53 changes: 53 additions & 0 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var webpack = require('webpack');

var helpers = require('./helpers');

const ENV = process.env.NODE_ENV = process.env.ENV = 'development';

module.exports = (env, argv) => {

// Set ENV depending on wepack mode (contained in argv passed by webpack --mode)
const ENV = process.env.NODE_ENV = process.env.ENV = (argv.mode === 'development') ? 'development' : 'production';

return {
output: {
path: helpers.root('dist'),
filename: '[name].js',
libraryTarget: 'umd',
library: '[name]',
umdNamedDefine: true,
globalObject: `(typeof self !== 'undefined' ? self : this)`

},

entry: {
'search-query-parser': helpers.root('lib', 'search-query-parser.ts')
},

devtool: ENV === 'development' ? 'eval-cheap-module-source-map': 'source-map',

resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
use: [
{
loader: 'awesome-typescript-loader',
options: { configFileName: helpers.root('tsconfig.json') }
}
]
}
]

},

optimization: {
minimize: true
}

}

}
32 changes: 32 additions & 0 deletions dist/search-query-parser.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*!
* search-query-parser.js
* Copyright(c) 2014-2019
* MIT Licensed
*/
export declare type SearchParserOptions = {
offsets?: boolean;
tokenize?: boolean;
keywords?: string[];
ranges?: string[];
alwaysArray?: boolean;
};
export declare type SearchParserOffset = (SearchParserKeyWordOffset | SearchParserTextOffset) & {
offsetStart: number;
offsetEnd: number;
};
export declare type SearchParserKeyWordOffset = {
keyword: string;
value: string;
};
export declare type SearchParserTextOffset = {
text: string;
};
export declare type SearchParserDictionary = {
[key: string]: any;
};
export declare type SearchParserResult = SearchParserDictionary & {
text: string | string[];
offsets?: SearchParserOffset[];
exclude?: SearchParserDictionary;
};
export declare function parse(string: string, options: SearchParserOptions): SearchParserResult | string;
7 changes: 7 additions & 0 deletions dist/search-query-parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/search-query-parser.js.map

Large diffs are not rendered by default.

32 changes: 0 additions & 32 deletions index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion index.js

This file was deleted.

Loading