Giter Site home page Giter Site logo

npm-js-utils's Introduction

JavaScript Utilities

A library of constants and functions that should work in any

  • typescript
  • ecmascript
  • javascript

The intention is that the library should not depend upon any node-module that doesn't work in at least these environments:

  • Enonic XP serverside code
  • Modern browser
  • Newest LTS version of Node

It should work in at least these frameworks:

  • React

Examples

DSL

import {storage} from '@enonic/js-utils';

const and = storage.query.dsl.and;
const bool = storage.query.dsl.bool;
const fulltext = storage.query.dsl.fulltext;
const ngram = storage.query.dsl.ngram;
const stemmed = storage.query.dsl.stemmed;

const fields = 'url^1.2,title^1.1,text';
const searchString = 'fun video';
const operator = 'OR';
const query = bool(and(
	fulltext(fields, searchString, operator),
	stemmed(fields, searchString, operator, 'no', 0.9),
	stemmed(fields, searchString, operator, 'en', 0.8),
	ngram(fields, searchString, operator, 0.7)
));
console.debug(JSON.stringify(query, null, 4));

/*
{
	"boolean": {
		"must": [
			{
				"boolean": {
					"should": [
						{
							"fulltext": {
								"fields": [
									"url"
								],
								"query": "fun video",
								"operator": "OR",
								"boost": 1.2
							}
						},
						{
							"fulltext": {
								"fields": [
									"title"
								],
								"query": "fun video",
								"operator": "OR",
								"boost": 1.1
							}
						},
						{
							"fulltext": {
								"fields": [
									"text"
								],
								"query": "fun video",
								"operator": "OR"
							}
						}
					]
				}
			},
			{
				"boolean": {
					"should": [
						{
							"stemmed": {
								"fields": [
									"url"
								],
								"query": "fun video",
								"operator": "OR",
								"language": "no",
								"boost": 1.08
							}
						},
						{
							"stemmed": {
								"fields": [
									"title"
								],
								"query": "fun video",
								"operator": "OR",
								"language": "no",
								"boost": 0.9900000000000001
							}
						},
						{
							"stemmed": {
								"fields": [
									"text"
								],
								"query": "fun video",
								"operator": "OR",
								"language": "no",
								"boost": 0.9
							}
						}
					]
				}
			},
			{
				"boolean": {
					"should": [
						{
							"stemmed": {
								"fields": [
									"url"
								],
								"query": "fun video",
								"operator": "OR",
								"language": "en",
								"boost": 0.96
							}
						},
						{
							"stemmed": {
								"fields": [
									"title"
								],
								"query": "fun video",
								"operator": "OR",
								"language": "en",
								"boost": 0.8800000000000001
							}
						},
						{
							"stemmed": {
								"fields": [
									"text"
								],
								"query": "fun video",
								"operator": "OR",
								"language": "en",
								"boost": 0.8
							}
						}
					]
				}
			},
			{
				"boolean": {
					"should": [
						{
							"ngram": {
								"fields": [
									"url"
								],
								"query": "fun video",
								"operator": "OR",
								"boost": 0.84
							}
						},
						{
							"ngram": {
								"fields": [
									"title"
								],
								"query": "fun video",
								"operator": "OR",
								"boost": 0.77
							}
						},
						{
							"ngram": {
								"fields": [
									"text"
								],
								"query": "fun video",
								"operator": "OR",
								"boost": 0.7
							}
						}
					]
				}
			}
		]
	}
};
*/

Filter

import {addQueryFilter} from '@enonic/js-utils';

const filters = addQueryFilter({
	filter: {
		exists: {
			field: 'required'
		}
	}
});
/*{
	boolean: {
		must: {
			exists: {
				field: 'required'
			}
		}
	}
}*/

Release

git tag vX.Y.Z
git push origin vX.Y.Z

This will trigger release & publish on NPM.

npm-js-utils's People

Contributors

comlock avatar dependabot[bot] avatar alansemenov avatar

Watchers

Jørgen Sivesind avatar Thomas Sigdestad avatar Morten Ø. Eriksen avatar James Cloos avatar Sergey Rymsha avatar  avatar  avatar

npm-js-utils's Issues

object.entries()

polyfill for Object.entries and Object.values which doesn't exist in ES5

Could the testing/mocking functonality be its own repo?

The testing part of this project could be moved out in its own repo.

Requiring a utils package/lib does not give a good indication that it is used for testing

Requiring a enonic-js-test package/lib does give a good indication thats its purpose is testing.

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.