Skip to content

Latest commit

 

History

History
247 lines (194 loc) · 3.95 KB

quickstart.md

File metadata and controls

247 lines (194 loc) · 3.95 KB

jaqt: Getting Started

prerequisites

jaqt is a pure or vanilla javascript library, with no other dependencies.

node

jaqt requires node version 13.2 or higher, as it uses the Proxy class and ES6 imports extensively.

Install it like this:

npm install @muze-nl/jaqt

Then use it like this:

import * as jaqt from '@muze-n;/jaqt'

browsers

jaqt will work on any modern browser (as of 2024) with support for ES6 modules and the Proxy class.

Either install it in your own project using npm:

npm install jaqt

Or use it directly from a CDN like jsdeliver.net:

<script type="module">
	import * as jaqt from 'https://cdn.jsdelivr.net/npm/@muze-nl/jaqt/src/jaqt.mjs'
</script>

importing all jaqt methods as global functions

In the remainder of the documentation, we're using the following import statement to make all methods available as global functions. This makes all the examples shorter and easier to read:

import { _, from, not, anyOf, allOf, asc, desc, sum, avg, count, max, min, one, many, first } from '@muze-nl/jaqt'

First steps

Lets define a very simple dataset:

let data = {
	people: [
		{
			name: 'Luke',
			lastName: 'Skywalker',
			height: 172,
			gender: 'male'
		},
		{
			name: 'Darth',
			lastName: 'Vader',
			height: 202,
			gender: 'male'
		},
		{
			name: 'Leia',
			lastName: 'Organa',
			height: 150,
			gender: 'female'
		},
		{
			name: 'R2-D2',
			height: 96,
			gender: 'n/a'
		}
	]
}

select

Now list the name of all people, like this:

from(data.people).select({
	name: _
})

and the result will be:

[
	{
		name: 'Luke'
	},
	{
		name: 'Darth'
	},
	{
		name: 'Leia'
	},
	{
		name: 'R2-D2'
	}
]

Read more about the features of select here

where

You can also filter the results based on a property, e.g:

from(data.people).where({
	gender: 'male'
}).select({
	name: _
})

And the result will be:

[
	{
		name: 'Luke'
	},
	{
		name: 'Darth'
	}
]

Read more about the features of where here

orderBy

You can order results by one or more properties:

from(data.people).orderBy({
	name: asc
}).select({
	name: _
})

Which will result in:

[
	{
		name: 'Darth'
	},
	{
		name: 'Leia'
	},
	{
		name: 'Luke'
	},
	{
		name: 'R2-D2'
	}
]

Read more about the features of orderBy here

groupBy

Finally you can group results based on a property:

from(data.people).select({
	name: _,
	gender: _
}).groupBy({
	gender: _
})

And this results in:

{
	male: [
		{
			name: "Luke",
			gender: "male"
		},
		{
			name: "Darth",
			gender: "male"
		}
	],
	female: [
		{
			name: "Leia",
			gender: "female"
		}
	],
	"n/a": [
		{
			name: "R2-D2",
			gender: "n/a"
		}
	]
}

Read more about the features of groupBy here

javascript array functions

jaqt is just a javascript library, and from() wraps objects and arrays with a Proxy. This means that you can still use normal javascript array methods, for example.

from(data.people)
.slice(0,2)
.select({
	name: _
})

Which will result in:

[
	{
		name: "Luke"
	},
	{
		name: "Darth"
	}
]

If you call from() on an array, the select,where and orderBy methods will return an array Proxy, with all array methods available. groupBy returns an object Proxy, with keys grouping the values.

If you call from() on an object, the select method will return an object Proxy. The where, orderBy and groupBy methods aren't available in this case.