• Stars
    star
    168
  • Rank 217,624 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

μ is a JavaScript library for encoding/decoding state (JavaScript object) in URL

μ Build Status

Without μ: http://app.com/url#%7B%22lookingFor%22:%22bride%22,%22age%22:%5B25,30%5D,%22religion%22:%22Hindu%22,%22motherTongue%22:%22Bengali%22,%22onlyProfileWithPhoto%22:true%7D

With μ: http://app.com/url#bHhc9I-aqa

μ is a JavaScript library for encoding/decoding state (JavaScript object) in URL. Define a spec for the state, based on which the encoding is done. Manage the state with versioning.

Example

Import the library

import {fromJson, encode, decode} from "u";

Define the spec.

var spec = {
        lookingFor: ['oneOf', 'bride', 'groom'],
        age: ['tuple', ['integer'] /* min */, ['integer'] /* max */],
        religion: ['oneOf', 'Hindu', 'Muslim', 'Christian', 'Sikh', 'Parsi', 'Jain', 'Buddhist', 'Jewish', 'No Religion', 'Spiritual', 'Other'],
        motherTongue: ['oneOf', 'Assamese', 'Bengali', 'English', 'Gujarati', 'Hindi', 'Kannada', 'Konkani', 'Malayalam', 'Marathi', 'Marwari', 'Odia', 'Punjabi', 'Sindhi', 'Tamil', 'Telugu', 'Urdu'],
        onlyProfileWithPhoto: ['boolean']
};

var v1 = fromJson(1, spec);

Encode the object/state.

var encodedv1 = encode(v1, {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true});
//=> 'bHhc9I-aqa'
decode([v1], encodedv1) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true});

Update your spec, as your application state space grows. Use versioning to encode/decode state.

var newSpec = _.extend({}, spec, {
        maritialStatus: ['oneOf', "Doesn't Matter", 'Never Married', 'Divorced', 'Widowed', 'Awaiting Divorce', 'Annulled']
});
var v2 = fromJson(2, newSpec, function (old) {
        old.maritialStatus = "Doesn't Matter";
        return old;
});

decode([v1, v2], encodedv1) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: "Doesn't Matter"});
var encodedv2 = encode(v2, {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: 'Never Married'});
//=> 'cHlc9I-aHaa'
decode([v1, v2], encodedv2) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: 'Never Married'});

API

fromJson(version, spec, [migrate])

version - spec version number
spec - used to define the structure and domain of the data.

structure
object is defined using { key: specForValue, ... }
array is defined using ['array', specForValue ]
tuple is defined using ['tuple', specForValueAtIndexZero, specForValueAtIndexOne, ...]

domain
domain is defined using [domainName, arg1, arg2, ...]

Domain Args Description
oneOf allowed values can be considered similar to enum. As we only encode the index position, the value could be anything
integer any integer
boolean true or false
fixedchar Size of the string fixed length string
varchar variable length string

migrate - a function that will get called in case where you decode an object encoded using older spec. For example, there are three versions v1, v2, v3 and you try to decode the string encoded using v1, then the migrate method in v2 and v3 will get called with the decoded value.

encode(coder, object)

coder - coder created using fromJson
object - object that needs to encoded

decode(coders, blob)

coders - array of coder.
blob - the string that is returned by encode.

More Repositories

1

paisa

Paisa – Personal Finance Manager. https://paisa.fyi demo: https://demo.paisa.fyi
TypeScript
1,679
star
2

tide

Tide - TypeScript Interactive Development Environment for Emacs
Emacs Lisp
1,444
star
3

webify

webfont generator - converts ttf to woff, eot and svg
Haskell
434
star
4

fake_dynamo

local hosted, inmemory Amazon DynamoDB emulator.
Ruby
156
star
5

monky

Magit for Hg
Emacs Lisp
152
star
6

jquery-doc.el

jQuery api documentation interface for emacs
Emacs Lisp
56
star
7

fdb

FoundationDB client for Elixir
Elixir
50
star
8

zstream

An elixir library to write and read ZIP file in a streaming fashion
Elixir
46
star
9

tree-sitter-elixir

C
43
star
10

barlix

Barcode generator for Elixir
Elixir
41
star
11

memcachex

Memcached client for Elixir
Elixir
37
star
12

exunit.el

Emacs ExUnit test runner
Emacs Lisp
36
star
13

ex_unit_span

An ExUnit formatter to visualize test execution and find bottlenecks in your test suite.
Elixir
27
star
14

randex

An Elixir library to generate random strings that match the given Regex
Elixir
17
star
15

neato

NES emulator
Go
15
star
16

exq_limit

Exq Rate Limiter
Elixir
10
star
17

rails-log-mode

Emacs Major mode for viewing Rails log files
Emacs Lisp
9
star
18

ananthakumaran.github.com

My Blog
JavaScript
8
star
19

toxiproxy-elixir

elixir client for Toxiproxy
Elixir
7
star
20

xlsx_stream

Streaming XLSX builder
Elixir
6
star
21

eopl

eopl using clojure
Clojure
6
star
22

imagebundler-wicket

imagebundler for Apache Wicket
Ruby
6
star
23

socialsite

A social network for students
Java
5
star
24

redix_sentinel

Redix with sentinel support
Elixir
5
star
25

exq_batch

Batch plugin for Exq library
Elixir
5
star
26

logstash-filter-elasticsearchslowlog

Elasticsearch Slowlog Logstash Plugin
Ruby
4
star
27

euler

solutions for problems in project euler
Scala
3
star
28

fdb_layer

Elixir
3
star
29

SICP

my solutions to the exercises in SICP
Scheme
3
star
30

visualization

JavaScript
3
star
31

jade-mode

Emacs major mode for editing Jade files
Emacs Lisp
3
star
32

avro_utils

Elixir Utility library to convert term to BigQuery compatible json based on avro schema
Elixir
2
star
33

xkeysnail

Python
2
star
34

FileOsculator

A desktop application used to chat and share files with the other users on the LAN
Java
2
star
35

opentelemetry_exq

Elixir
1
star
36

ecto_yaml

Elixir
1
star
37

erbot

IRC bot
Elixir
1
star
38

gate

Erlang bindings for git natural language date parser
C
1
star
39

hopfli

Haskell bindings for Zopfli Compression Algorithm
Haskell
1
star
40

scala-mode

mirror of scala-mode
Emacs Lisp
1
star
41

record

JavaScript
1
star
42

pan

Elixir
1
star
43

advent-of-code

Prolog
1
star
44

assistant

Gitlab merge bot
Elixir
1
star