You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
278 lines
10 KiB
278 lines
10 KiB
{ |
|
"cells": [ |
|
{ |
|
"cell_type": "markdown", |
|
"id": "19016eb2-043d-446f-9949-6ba6e3eda6ec", |
|
"metadata": {}, |
|
"source": [ |
|
"# μ\n", |
|
"μ 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.\n", |
|
"\n", |
|
"https://github.com/ananthakumaran/u" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 5, |
|
"id": "dcc14dfe-fcdc-43f1-9c6d-3538d8f7ec28", |
|
"metadata": { |
|
"collapsed": true, |
|
"jupyter": { |
|
"outputs_hidden": true, |
|
"source_hidden": true |
|
}, |
|
"tags": [] |
|
}, |
|
"outputs": [ |
|
{ |
|
"name": "stdout", |
|
"output_type": "stream", |
|
"text": [ |
|
"{\n", |
|
" version: 1,\n", |
|
" spec: { encode: [Function: encode], decode: [Function: decode] },\n", |
|
" jsonSpec: {\n", |
|
" lookingFor: [ 'oneOf', 'bride', 'groom' ],\n", |
|
" age: [ 'tuple', [Array], [Array] ],\n", |
|
" religion: [\n", |
|
" 'oneOf', 'Hindu',\n", |
|
" 'Muslim', 'Christian',\n", |
|
" 'Sikh', 'Parsi',\n", |
|
" 'Jain', 'Buddhist',\n", |
|
" 'Jewish', 'No Religion',\n", |
|
" 'Spiritual', 'Other'\n", |
|
" ],\n", |
|
" motherTongue: [\n", |
|
" 'oneOf', 'Assamese',\n", |
|
" 'Bengali', 'English',\n", |
|
" 'Gujarati', 'Hindi',\n", |
|
" 'Kannada', 'Konkani',\n", |
|
" 'Malayalam', 'Marathi',\n", |
|
" 'Marwari', 'Odia',\n", |
|
" 'Punjabi', 'Sindhi',\n", |
|
" 'Tamil', 'Telugu',\n", |
|
" 'Urdu'\n", |
|
" ],\n", |
|
" onlyProfileWithPhoto: [ 'boolean' ]\n", |
|
" },\n", |
|
" encodedVersion: 'b',\n", |
|
" migrate: [Function (anonymous)]\n", |
|
"}\n" |
|
] |
|
}, |
|
{ |
|
"data": { |
|
"text/plain": [ |
|
"undefined" |
|
] |
|
}, |
|
"execution_count": 5, |
|
"metadata": {}, |
|
"output_type": "execute_result" |
|
} |
|
], |
|
"source": [ |
|
"import {fromJson, encode, decode} from \"./resources/u\"\n", |
|
"var spec = {\n", |
|
" lookingFor: ['oneOf', 'bride', 'groom'],\n", |
|
" age: ['tuple', ['integer'] /* min */, ['integer'] /* max */],\n", |
|
" religion: ['oneOf', 'Hindu', 'Muslim', 'Christian', 'Sikh', 'Parsi', 'Jain', 'Buddhist', 'Jewish', 'No Religion', 'Spiritual', 'Other'],\n", |
|
" motherTongue: ['oneOf', 'Assamese', 'Bengali', 'English', 'Gujarati', 'Hindi', 'Kannada', 'Konkani', 'Malayalam', 'Marathi', 'Marwari', 'Odia', 'Punjabi', 'Sindhi', 'Tamil', 'Telugu', 'Urdu'],\n", |
|
" onlyProfileWithPhoto: ['boolean']\n", |
|
"};\n", |
|
"\n", |
|
"var v1 = fromJson(1, spec);\n", |
|
"console.log(v1)" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 7, |
|
"id": "b6ba3e69-75a8-4e7b-817f-eafabd3c0214", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"data": { |
|
"text/plain": [ |
|
"{\n", |
|
" age: [ 25, 30 ],\n", |
|
" lookingFor: 'bride',\n", |
|
" motherTongue: 'Bengali',\n", |
|
" onlyProfileWithPhoto: true,\n", |
|
" religion: 'Hindu'\n", |
|
"}" |
|
] |
|
}, |
|
"execution_count": 7, |
|
"metadata": {}, |
|
"output_type": "execute_result" |
|
} |
|
], |
|
"source": [ |
|
"import {fromJson, encode, decode} from \"./resources/u\"\n", |
|
"var encodedv1 = encode(v1, {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true});\n", |
|
"//=> 'bHhc9I-aqa'\n", |
|
"decode([v1], encodedv1) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true});\n" |
|
] |
|
}, |
|
{ |
|
"cell_type": "code", |
|
"execution_count": 13, |
|
"id": "666b73b0-58e6-4d13-a09a-ec88b2f048d5", |
|
"metadata": {}, |
|
"outputs": [ |
|
{ |
|
"data": { |
|
"text/plain": [ |
|
"{\n", |
|
" age: [ 25, 30 ],\n", |
|
" lookingFor: 'bride',\n", |
|
" maritialStatus: 'Never Married',\n", |
|
" motherTongue: 'Bengali',\n", |
|
" onlyProfileWithPhoto: true,\n", |
|
" religion: 'Hindu'\n", |
|
"}" |
|
] |
|
}, |
|
"execution_count": 13, |
|
"metadata": {}, |
|
"output_type": "execute_result" |
|
} |
|
], |
|
"source": [ |
|
"import {fromJson, encode, decode} from \"./resources/u\"\n", |
|
"import _ from \"lodash\"\n", |
|
"var newSpec = _.extend({}, spec, {\n", |
|
" maritialStatus: ['oneOf', \"Doesn't Matter\", 'Never Married', 'Divorced', 'Widowed', 'Awaiting Divorce', 'Annulled']\n", |
|
"});\n", |
|
"var v2 = fromJson(2, newSpec, function (old) {\n", |
|
" old.maritialStatus = \"Doesn't Matter\";\n", |
|
" return old;\n", |
|
"});\n", |
|
"\n", |
|
"decode([v1, v2], encodedv1) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: \"Doesn't Matter\"});\n", |
|
"var encodedv2 = encode(v2, {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: 'Never Married'});\n", |
|
"//=> 'cHlc9I-aHaa'\n", |
|
"decode([v1, v2], encodedv2) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: 'Never Married'});" |
|
] |
|
}, |
|
{ |
|
"cell_type": "markdown", |
|
"id": "cb0e6939-675a-4345-9e2f-27922fc888d3", |
|
"metadata": {}, |
|
"source": [ |
|
"# μ [![Build Status](https://travis-ci.org/ananthakumaran/u.svg?branch=master)](https://travis-ci.org/ananthakumaran/u)\n", |
|
"\n", |
|
"Without μ:\n", |
|
"`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`\n", |
|
"\n", |
|
"With μ:\n", |
|
"`http://app.com/url#bHhc9I-aqa`\n", |
|
"\n", |
|
"μ is a JavaScript library for encoding/decoding state (JavaScript\n", |
|
"object) in URL. Define a spec for the state, based on which the\n", |
|
"encoding is done. Manage the state with versioning.\n", |
|
"\n", |
|
"## Example\n", |
|
"\n", |
|
"Import the library\n", |
|
"\n", |
|
"`import {fromJson, encode, decode} from \"u\";`\n", |
|
"\n", |
|
"Define the spec.\n", |
|
"\n", |
|
"```javascript\n", |
|
"var spec = {\n", |
|
" lookingFor: ['oneOf', 'bride', 'groom'],\n", |
|
" age: ['tuple', ['integer'] /* min */, ['integer'] /* max */],\n", |
|
" religion: ['oneOf', 'Hindu', 'Muslim', 'Christian', 'Sikh', 'Parsi', 'Jain', 'Buddhist', 'Jewish', 'No Religion', 'Spiritual', 'Other'],\n", |
|
" motherTongue: ['oneOf', 'Assamese', 'Bengali', 'English', 'Gujarati', 'Hindi', 'Kannada', 'Konkani', 'Malayalam', 'Marathi', 'Marwari', 'Odia', 'Punjabi', 'Sindhi', 'Tamil', 'Telugu', 'Urdu'],\n", |
|
" onlyProfileWithPhoto: ['boolean']\n", |
|
"};\n", |
|
"\n", |
|
"var v1 = fromJson(1, spec);\n", |
|
"```\n", |
|
"\n", |
|
"Encode the object/state.\n", |
|
"\n", |
|
"```javascript\n", |
|
"var encodedv1 = encode(v1, {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true});\n", |
|
"//=> 'bHhc9I-aqa'\n", |
|
"decode([v1], encodedv1) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true});\n", |
|
"```\n", |
|
"\n", |
|
"Update your spec, as your application state space grows. Use versioning to\n", |
|
"encode/decode state.\n", |
|
"\n", |
|
"```javascript\n", |
|
"var newSpec = _.extend({}, spec, {\n", |
|
" maritialStatus: ['oneOf', \"Doesn't Matter\", 'Never Married', 'Divorced', 'Widowed', 'Awaiting Divorce', 'Annulled']\n", |
|
"});\n", |
|
"var v2 = fromJson(2, newSpec, function (old) {\n", |
|
" old.maritialStatus = \"Doesn't Matter\";\n", |
|
" return old;\n", |
|
"});\n", |
|
"\n", |
|
"decode([v1, v2], encodedv1) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: \"Doesn't Matter\"});\n", |
|
"var encodedv2 = encode(v2, {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: 'Never Married'});\n", |
|
"//=> 'cHlc9I-aHaa'\n", |
|
"decode([v1, v2], encodedv2) //=> {lookingFor: 'bride', age: [25, 30], religion: 'Hindu', motherTongue: 'Bengali', onlyProfileWithPhoto: true, maritialStatus: 'Never Married'});\n", |
|
"```\n", |
|
"\n", |
|
"## API\n", |
|
"\n", |
|
"### fromJson(version, spec, [migrate])\n", |
|
"\n", |
|
"**version** - spec version number \n", |
|
"**spec** - used to define the structure and domain of the data.\n", |
|
"\n", |
|
"*structure* \n", |
|
"object is defined using { key: specForValue, ... } \n", |
|
"array is defined using ['array', specForValue ] \n", |
|
"tuple is defined using ['tuple', specForValueAtIndexZero, specForValueAtIndexOne, ...] \n", |
|
"\n", |
|
"*domain* \n", |
|
"domain is defined using [domainName, arg1, arg2, ...]\n", |
|
"\n", |
|
"| Domain | Args | Description |\n", |
|
"---------|------|-------------|\n", |
|
"| oneOf | allowed values | can be considered similar to enum. As we only encode the index position, the value could be anything |\n", |
|
"| integer | | any integer |\n", |
|
"| boolean | | true or false |\n", |
|
"| fixedchar | Size of the string | fixed length string |\n", |
|
"| varchar | | variable length string |\n", |
|
"\n", |
|
"**migrate** - a function that will get called in case where you decode\n", |
|
"an object encoded using older spec. For example, there are three\n", |
|
"versions v1, v2, v3 and you try to decode the string encoded using v1,\n", |
|
"then the migrate method in v2 and v3 will get called with the decoded\n", |
|
"value.\n", |
|
"\n", |
|
"### encode(coder, object)\n", |
|
"\n", |
|
"**coder** - coder created using fromJson \n", |
|
"**object** - object that needs to encoded \n", |
|
"\n", |
|
"### decode(coders, blob)\n", |
|
"\n", |
|
"**coders** - array of coder. \n", |
|
"**blob** - the string that is returned by encode. \n" |
|
] |
|
} |
|
], |
|
"metadata": { |
|
"kernelspec": { |
|
"display_name": "jp-Babel (Node.js)", |
|
"language": "babel", |
|
"name": "babel" |
|
}, |
|
"language_info": { |
|
"file_extension": ".js", |
|
"mimetype": "application/javascript", |
|
"name": "javascript", |
|
"version": "17.9.0" |
|
} |
|
}, |
|
"nbformat": 4, |
|
"nbformat_minor": 5 |
|
}
|
|
|