0.3.1 • Published 1 year ago

query-pack v0.3.1

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

query-pack


Encode - Decode Query Parameter

Usage

Installation:

npm install query-pack

String example

Encodes string value:

import  { encode } from 'query-pack';
...
const qParam = encode('Hey, how are you?')
const url = `https://mydomain.com?q=${qParam}`;
console.log(url);
// =>  https://mydomain.com?q=UheyCWhowWareWyouQ

Decodes string value:

import  { decode } from 'query-pack';
...
let params = (new URL(document.location)).searchParams;
let qParam = params.get("q");
const value = decode(qParam);
console.log(value);
// =>  Hey, how are you?

Object example

Encodes object value:

import  { encode } from 'query-pack';
...
const qParam = encode({
  id: 1,
  name: "Team 1",
  captain: "zak",
})
const url = `https://mydomain.com?q=${qParam}`;
console.log(url);
// =>  https://mydomain.com?q=1XidN1YnameSUteamW1YcaptainSzak

Decodes object value:

import  { decode } from 'query-pack';
...
let params = (new URL(document.location)).searchParams;
let qParam = params.get("q");
const value = decode(qParam);
console.log(value);
// =>  {
//   id: 1,
//   name: "Team 1",
//   captain: "zak",
// }

Options

Options is an encoding settings passed as a second parameter to the encode or decode functions. Has PackOptions type.

NameDescription
fieldsReplaces the names of the object's fields into numbers in order to make encoded result shorter. Example of using fields optimization
valuesReplaces the values of the object's field into numbers or short strings in order to make encoded result shorter. Example of using values optimization
includeUndefinedPropertyIndicates if property of object with undefined value must be included into encoded result. Default value is false

Fields Optimization

Here we have a team object. Its field's names are replaced with numbers (id=> 1, ...). Since numbers are shorter and as result we will have shorter encoded result.

import  { encode, decode, PackOptions } from 'query-pack';
...
const packOptions: PackOptions = {
    fields:{
        id: 1,
        name: 2,
        captain: 3
    }
}
...
const qParam = encode({
    id: 1,
    name: "Team 1",
    captain: "zak",
}, packOptions);

console.log(qParam);
// =>  1X1N1Y2SUteamW1Y3Szak

const team = decode(qParam, packOptions);
console.log(team);
// =>  {
//   id: 1,
//   name: "Team 1",
//   captain: "zak",
// }

Values Optimization

Here we have a player object with level field. Possible values for it are strings: unknown, notbad, normal, good, star. We replacing them with numbers. In our case the player object has good level value. So while encoding it would receive value of 4. This makes encoded result shorter.

import  { encode, decode, PackOptions } from 'query-pack';
...
const packOptions: PackOptions = {
    values:{
        level: {
            unknown: 1,
            notbad: 2,
            normal: 3,
            good: 4,
            star: 5,
        },
    }
}
...
const qParam = encode({
    name: "zak",
    level: "good",
}, packOptions);

console.log(qParam);
// =>  1XnameSzakYlevelS4

const team = decode(qParam, packOptions);
console.log(team);
// =>  {
//   name: "zak",
//   level: "good",
// }

Philosophy

The maximum length of URL is 2048 characters. This is enough space for encoding data for small or medium page. What's more, in this case you don't need to have database or API.

There is and example of using query-pack. This is the tournament team constructor witch generates the tournament data into URL's query parameter. That's it, no API or database the tournament data is stored into the URL. This link has 512 chars and this is enough for encoding information about 4 teams and 24 players, games between teams, scheduel and results.

Base

query-pack encodesstring, number, boolean, array, object values into a string for using it in URL's query paramter.

This is kind of alternative of using encodeURIComponent and decodeURIComponent, but with differences. The primitive types like string, number, and boolean query-pack makes shorter in coparances to encodeURIComponent function with the Primitive Strategies. Also, the query-pack can encode complex types like object or array with the Complex Strategies where the encodeURIComponent does not support compolex types.

Primitive Strategies

Upper Case

In URL encoding there are not replacing chars: numbers, upper and lower case English letters, -, _, ., ~. All other has encodes with extra chars, for instance @ encodes as %40. zqip replace upper case chars on the text with U + upper case char or U{X} + X upper case chars.

TextEncoded
HelloUhello
HTMLU5html

This helps to release all other upper case English letters for further encoding strategies.

String

Most used chars are replaced with upper case chars.

CharURL Encodingquery-pack
space%20W
'%27H
(%28G
)%29R
;%3BT
:%3AK
@%40M
=%3DJ
+%2BP
,%2CC
?%3FQ
"%22H
StringURL Encodingquery-pack
TextHey, how are you?Hey%2C%20how%20are%20you%3FUheyCWhowWareWyouQ
Length172718

Number

Decimal numbers replaces with based32 numbers.

Numberquery-pack
Value123456.7893oi0.ol
Length107

Boolean

Replaces with 0 and 1.

Booleanquery-pack
false0
true1

Complex Strategies

Complex strategy provides the way of converting into a string the object with properties or array of items. To list values need to split them somehow and indicate their type. The below table contains type indicators:

Typequery-pack type indicator
stringS
numberN
booleanB
objectO
arrayA
nullL
undefinedF

Next table contains splitters:

query-pack splitterDescription
YProperty
XObject

Array

TypeArrayzqip
Number12, 34, 56NcN12N1o
String"cat", "dog", "mouse"ScatSdogSmouse
Number, String, Boolean12, "dog", trueNcSdogB1

Object

Let's imaging we have the team object:

{
  id: 1,
  name: "Team 1",
  captain: "zak",
}

After encoding into a string:

idN1YnameSUteamW1YcaptainSzak
0.3.1

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.1.0

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago