Commit 02333f8c authored by Tristan Cavelier's avatar Tristan Cavelier

Merge remote-tracking branch 'origin/keys-jiodate-async'

parents dc338ff8 d42fadc0
......@@ -27,6 +27,9 @@ module.exports = function (grunt) {
errorsOnly: true
}
},
jiodate: {
src: ['src/jio.date/jiodate.js']
},
tests: {
src: ['test/**/*.js'],
options: {
......@@ -82,6 +85,10 @@ module.exports = function (grunt) {
src: 'jio.js', // '<%= pkg.name %>.js'
dest: 'jio.min.js'
},
jiodate: {
src: 'src/jio.date/jiodate.js',
dest: 'jiodate.min.js'
},
queries: {
src: 'complex_queries.js',
dest: 'complex_queries.min.js'
......
......@@ -4,10 +4,14 @@ QUERIES_DIR = src/queries
# files
JIO = jio.js
JIO_MIN = jio.min.js
JIODATE_MIN = jiodate.min.js
COMPLEX = complex_queries.js
COMPLEX_MIN = complex_queries.min.js
PARSER_PAR = $(QUERIES_DIR)/core/parser.par
PARSER_OUT = $(QUERIES_DIR)/build/parser.js
UGLIFY = ./node_modules/grunt-contrib-uglify/node_modules/uglify-js/bin/uglifyjs
ZIP_FULL = jio.zip
ZIP_MINI = jio-min.zip
# npm install jscc-node
JSCC_CMD = node ./node_modules/jscc-node/jscc.js -t ./node_modules/jscc-node/driver_node.js_
......@@ -18,6 +22,68 @@ compile:
mkdir -p $(dir $(PARSER_OUT))
$(JSCC_CMD) -o $(PARSER_OUT) $(PARSER_PAR)
TMPDIR := $(shell mktemp -d)
zip:
@echo "Preparing $(ZIP_FULL)"
@mkdir $(TMPDIR)/jio
@mkdir $(TMPDIR)/jio/storage
@cp jio.js $(TMPDIR)/jio/
@cp complex_queries.js $(TMPDIR)/jio/
@cp src/sha1.amd.js $(TMPDIR)/jio/
@cp src/sha2.amd.js $(TMPDIR)/jio/
@cp src/sha256.amd.js $(TMPDIR)/jio/
@cp lib/rsvp/rsvp-custom.js $(TMPDIR)/jio/
@cp lib/rsvp/rsvp-custom.amd.js $(TMPDIR)/jio/
@cp lib/jquery/jquery.js $(TMPDIR)/jio/
@cp lib/require/require.js $(TMPDIR)/jio/
@cp src/jio.storage/localstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/davstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/erp5storage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/indexstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/gidstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/replicatestorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/cryptstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/revisionstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/replicaterevisionstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/s3storage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/splitstorage.js $(TMPDIR)/jio/storage/
@cp src/jio.storage/xwikistorage.js $(TMPDIR)/jio/storage/
@cd $(TMPDIR) && zip -q $(ZIP_FULL) -r jio
@mv $(TMPDIR)/$(ZIP_FULL) ./
@rm -rf $(TMPDIR)/jio
@echo "Preparing $(ZIP_MINI)"
@mkdir $(TMPDIR)/jio
@mkdir $(TMPDIR)/jio/storage
@echo "Minimizing JS..."
@cp jio.min.js $(TMPDIR)/jio/
@cp complex_queries.min.js $(TMPDIR)/jio/
@$(UGLIFY) src/sha1.amd.js >$(TMPDIR)/jio/sha1.amd.min.js 2>/dev/null
@$(UGLIFY) src/sha2.amd.js >$(TMPDIR)/jio/sha2.amd.min.js 2>/dev/null
@$(UGLIFY) src/sha256.amd.js >$(TMPDIR)/jio/sha256.amd.min.js 2>/dev/null
@$(UGLIFY) lib/rsvp/rsvp-custom.js >$(TMPDIR)/jio/rsvp-custom.min.js 2>/dev/null
@$(UGLIFY) lib/rsvp/rsvp-custom.amd.js >$(TMPDIR)/jio/rsvp-custom.amd.min.js 2>/dev/null
@cp lib/jquery/jquery.min.js $(TMPDIR)/jio/
@$(UGLIFY) lib/require/require.js >$(TMPDIR)/jio/require.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/localstorage.js >$(TMPDIR)/jio/storage/localstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/davstorage.js >$(TMPDIR)/jio/storage/davstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/erp5storage.js >$(TMPDIR)/jio/storage/erp5storage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/indexstorage.js >$(TMPDIR)/jio/storage/indexstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/gidstorage.js >$(TMPDIR)/jio/storage/gidstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/replicatestorage.js >$(TMPDIR)/jio/storage/replicatestorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/cryptstorage.js >$(TMPDIR)/jio/storage/cryptstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/revisionstorage.js >$(TMPDIR)/jio/storage/revisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/replicaterevisionstorage.js >$(TMPDIR)/jio/storage/replicaterevisionstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/s3storage.js >$(TMPDIR)/jio/storage/s3storage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/splitstorage.js >$(TMPDIR)/jio/storage/splitstorage.min.js 2>/dev/null
@$(UGLIFY) src/jio.storage/xwikistorage.js >$(TMPDIR)/jio/storage/xwikistorage.min.js 2>/dev/null
@cd $(TMPDIR) && zip -q $(ZIP_MINI) -r jio
@mv $(TMPDIR)/$(ZIP_MINI) ./
@rm -rf $(TMPDIR)
.phony: clean
clean:
find -name '*~' -delete
......@@ -25,6 +91,7 @@ clean:
realclean:
rm -f "$(JIO)"
rm -f "$(JIO_MIN)"
rm -f "$(JIODATE_MIN)"
rm -f "$(COMPLEX)"
rm -f "$(COMPLEX_MIN)"
rm -f "$(PARSER_OUT)"
......@@ -5,138 +5,144 @@
### Getting Started
To set up jIO include jio.js, dependencies and the connectors for the storages
you want to use in your page header (note that more dependencies may be required
To set up jIO you should include jio.js, dependencies and the connectors for the storages
you want to use in the HTML page header (note that more dependencies may be required
depending on type of storages being used):
<!-- jio + dependency -->
<script src="sha256.amd.js"></script>
<script src="rsvp-custom.js"></script>
<script src="jio.js"></script>
```html
<!-- jio + dependency -->
<script src="sha256.amd.js"></script>
<script src="rsvp-custom.js"></script>
<script src="jio.js"></script>
<!-- jio storage libraries -->
<script src="complex-queries.js"></script>
<script src="localstorage.js"></script>
<!-- jio storage libraries -->
<script src="complex-queries.js"></script>
<script src="localstorage.js"></script>
<script ...>
<script ...>
```
Then create your jIO instance like this:
// create a new jio (type = localStorage)
var jio_instance = jIO.createJIO({
"type": "local",
"username": "your_username",
"application_name": "your_application_name"
});
```javascript
// create a new jio (type = localStorage)
var jio_instance = jIO.createJIO({
"type": "local",
"username": "your_username",
"application_name": "your_application_name"
});
```
### Documents and Methods
Documents are JSON strings that contain *meta-data* (properties, like a filename)
Documents are JSON strings that contain *metadata* (properties, like a filename)
and *attachments* (optional content, for example *image.jpg*).
jIO exposes the following methods to *create*, *read*, *update* and *delete* documents
(for more information, including revision management and available options for
each method, please refer to the documentation):
// create and store new document
jio_instance.post({"title": "some title"}).
then(function (response) {
// console.log(response):
// {
// "result": "success",
// "id": "404aef5e-22cc-4a64-a292-37776c6464a3" // Generated id
// ...
// }
});
// create or update an existing document
jio_instance.put({"_id": "my_document", "title": "New Title"}).
then(function (response) {
// console.log(response):
// {
// "result": "success",
// "id": "my_document",
// ...
// }
});
// add an attachement to a document
jio_instance.putAttachment({"_id": "my_document", "_attachment": "its_attachment",
"_data": "abc", "_mimetype": "text/plain"}).
then(function (response) {
// console.log(response):
// {
// "result": "success",
// "id": "my_document",
// "attachment": "its_attachment"
// ...
// }
});
// read a document
jio_instance.get({"_id": "my_document"}).
then(function (response) {
// console.log(response);
// {
// "data": {
// "_id": "my_document",
// "title": "New Title",
// "_attachments": {
// "its_attachment": {
// "length": 3,
// "digest": "sha256-ba7816bf8f1cfea414140de5dae2223b0361a396177a9cb410ff61f2015ad",
// "content_type": "text/plain"
// }
// }
// },
// ...
// }
});
// read an attachement
jio_instance.getAttachment({"_id": "my_document", "_attachment": "its_attachment"}).
then(function (response) {
// console.log(response);
// {
// "data": Blob, // contains the attachment data + content type
// ...
// }
});
// delete a document and its attachment(s)
jio_instance.remove({"_id": "my_document"}).
then(function (response) {
// console.log(response):
// {
// "result": "success",
// "id": "my_document"
// }
});
// delete an attachement
jio_instance.removeAttachment({"_id": "my_document", "_attachment": "its_attachment"}).
then(function (response) {
// console.log(response):
// {
// "result": "success",
// "id": "my_document",
// "attachment": "its_attachment"
// }
});
// get all documents
jio_instance.allDocs().then(function (response) {
// console.log(response):
// {
// "data": {
// "total_rows": 1,
// "rows": [{
// "id": "my_document",
// "value": {}
// }]
// }
// }
});
```javascript
// create and store new document
jio_instance.post({"title": "some title"}).
then(function (response) {
// console.log(response);
// {
// "result": "success",
// "id": "404aef5e-22cc-4a64-a292-37776c6464a3" // Generated id
// ...
// }
});
// create or update an existing document
jio_instance.put({"_id": "my_document", "title": "New Title"}).
then(function (response) {
// console.log(response);
// {
// "result": "success",
// "id": "my_document",
// ...
// }
});
// add an attachement to a document
jio_instance.putAttachment({"_id": "my_document", "_attachment": "its_attachment",
"_data": "abc", "_mimetype": "text/plain"}).
then(function (response) {
// console.log(response);
// {
// "result": "success",
// "id": "my_document",
// "attachment": "its_attachment"
// ...
// }
});
// read a document
jio_instance.get({"_id": "my_document"}).
then(function (response) {
// console.log(response);
// {
// "data": {
// "_id": "my_document",
// "title": "New Title",
// "_attachments": {
// "its_attachment": {
// "length": 3,
// "digest": "sha256-ba7816bf8f1cfea414140de5dae2223b0361a396177a9cb410ff61f2015ad",
// "content_type": "text/plain"
// }
// }
// },
// ...
// }
});
// read an attachement
jio_instance.getAttachment({"_id": "my_document", "_attachment": "its_attachment"}).
then(function (response) {
// console.log(response);
// {
// "data": Blob, // contains the attachment data + content type
// ...
// }
});
// delete a document and its attachment(s)
jio_instance.remove({"_id": "my_document"}).
then(function (response) {
// console.log(response);
// {
// "result": "success",
// "id": "my_document"
// }
});
// delete an attachement
jio_instance.removeAttachment({"_id": "my_document", "_attachment": "its_attachment"}).
then(function (response) {
// console.log(response);
// {
// "result": "success",
// "id": "my_document",
// "attachment": "its_attachment"
// }
});
// get all documents
jio_instance.allDocs().then(function (response) {
// console.log(response);
// {
// "data": {
// "total_rows": 1,
// "rows": [{
// "id": "my_document",
// "value": {}
// }]
// }
// }
});
```
### Example
......@@ -144,50 +150,52 @@ each method, please refer to the documentation):
This is an example of how to store a video file with one attachment in local
storage. Note that attachments should be added after document creation.
// create a new localStorage
var jio_instance = jIO.createJIO({
"type": "local",
"username": "user",
"application_name": "app"
});
```javascript
// create a new localStorage
var jio_instance = jIO.createJIO({
"type": "local",
"username": "user",
"application_name": "app"
});
var my_video_blob = new Blob([my_video_binary_string], {
"type": "video/ogg"
});
var my_video_blob = new Blob([my_video_binary_string], {
"type": "video/ogg"
});
// post the document
jio_instance.post({
"_id" : "myVideo",
"title" : "My Video",
"format" : ["video/ogg", "vorbis", "HD"],
"language" : "en",
"description" : "Images Compilation"
}).then(function (response) {
// post the document
jio_instance.post({
"_id" : "myVideo",
"title" : "My Video",
"format" : ["video/ogg", "vorbis", "HD"],
"language" : "en",
"description" : "Images Compilation"
}).then(function (response) {
// add video attachment
return jio_instance.putAttachment({
"_id": "myVideo",
"_attachment": "video.ogv",
"_data": my_video_blob,
});
// add video attachment
return jio_instance.putAttachment({
"_id": "myVideo",
"_attachment": "video.ogv",
"_data": my_video_blob,
});
}).then(function (response) {
}).then(function (response) {
alert('Video Stored');
alert('Video Stored');
}, function (err) {
}, function (err) {
if (err.method === "post") {
alert('Error when posting the document description');
} else {
alert('Error when attaching the video');
}
if (err.method === "post") {
alert('Error when posting the document description');
} else {
alert('Error when attaching the video');
}
}, function (progression) {
}, function (progression) {
console.log(progression);
console.log(progression);
});
});
```
### Storage Locations
......@@ -199,20 +207,24 @@ The following storages are currently supported:
- LocalStorage (browser local storage)
// initialize a local storage
var jio_instance = jIO.createJIO({
"type" : "local",
"username" : "me"
});
```javascript
// initialize a local storage
var jio_instance = jIO.createJIO({
"type" : "local",
"username" : "me"
});
```
- DAVStorage (connect to webDAV, more information on the
[documentation](https://www.j-io.org/documentation/jio-documentation/))
// initialize a webDAV storage (without authentication)
var jio_instance = jIO.createJIO({
"type": "dav",
"url": "http://my.dav.srv/uploads"
});
```javascript
// initialize a webDAV storage (without authentication)
var jio_instance = jIO.createJIO({
"type": "dav",
"url": "http://my.dav.srv/uploads"
});
```
<!-- - xWiki storage (connect to xWiki) -->
......@@ -301,49 +313,51 @@ create your own connector, please also refer to the [documentation](https://www.
jIO uses complex-queries manager, which can be run on top of the allDocs()
method to query documents in the storage tree. A sample query would look like
this (note, that allDocs and complex queries cannot be run on every storage and
this (note that not all storages support allDocs and complex queries, and
that pre-querying of documents on distant storages should best be done
server-side):
// run allDocs with query option on an existing jIO
jio_instance.allDocs({
"query": '(fieldX: >= "string" AND fieldY: < "string")',
// records to display ("from to")
"limit": [0, 5],
// sort by
"sort_on": [[<string A>, 'descending']],
// fields to return in response
"select_list": [<string A>, <string B>]
}).then(function (response) {
// console.log(response):
// {
// "total_rows": 1,
// "rows": [{
// "id": <string>,
// "value": {
// <string A>: <string>,
// <string B>: <string>
// }
// }, { .. }]
// }
});
To find out more about complex queries, please refer to the documentation
```javascript
// run allDocs with query option on an existing jIO
jio_instance.allDocs({
"query": '(fieldX: >= "string" AND fieldY: < "string")',
// records to display ("from to")
"limit": [0, 5],
// sort by
"sort_on": [[<string A>, 'descending']],
// fields to return in response
"select_list": [<string A>, <string B>]
}).then(function (response) {
// console.log(response);
// {
// "total_rows": 1,
// "rows": [{
// "id": <string>,
// "value": {
// <string A>: <string>,
// <string B>: <string>
// }
// }, { .. }]
// }
});
```
To find out more about complex queries, please refer to the documentation.
### Task Management
jIO is running a task queue manager in the background which processes incoming
tasks according to set of defined rules. To find out more and including how to
tasks according to a set of defined rules. To find out more including how to
define your own execution rules, please refer to the documentation.
### Conflict Management
As jIO allows to manage and share documents across multiple storage locactions
it is likely for conflicts to occur (= multiple versions of a single document
As jIO allows to manage and share documents across multiple storage locactions,
conflicts may happen (i.e. multiple versions of a single document
existing in the storage tree). jIO manages conflicts by ensuring that every
version of a document is available on every storage and that conflicts are
accessible (and solvable) using the *conflicts: true* option when using the
respective jIO methods. For more info on conflicts and available options, please
related jIO methods. For more info on conflicts and available options, please
refer to the documentation.
### Crash-Proof
......
......@@ -97,17 +97,18 @@ function __NODEJS_lex( info )
switch( state )
{
case 0:
if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 8 ) || ( info.src.charCodeAt( pos ) >= 10 && info.src.charCodeAt( pos ) <= 31 ) || info.src.charCodeAt( pos ) == 33 || ( info.src.charCodeAt( pos ) >= 35 && info.src.charCodeAt( pos ) <= 39 ) || ( info.src.charCodeAt( pos ) >= 42 && info.src.charCodeAt( pos ) <= 57 ) || info.src.charCodeAt( pos ) == 59 || ( info.src.charCodeAt( pos ) >= 63 && info.src.charCodeAt( pos ) <= 64 ) || ( info.src.charCodeAt( pos ) >= 66 && info.src.charCodeAt( pos ) <= 77 ) || ( info.src.charCodeAt( pos ) >= 80 && info.src.charCodeAt( pos ) <= 254 ) ) state = 1;
if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 8 ) || ( info.src.charCodeAt( pos ) >= 10 && info.src.charCodeAt( pos ) <= 31 ) || ( info.src.charCodeAt( pos ) >= 35 && info.src.charCodeAt( pos ) <= 39 ) || ( info.src.charCodeAt( pos ) >= 42 && info.src.charCodeAt( pos ) <= 57 ) || info.src.charCodeAt( pos ) == 59 || ( info.src.charCodeAt( pos ) >= 63 && info.src.charCodeAt( pos ) <= 64 ) || ( info.src.charCodeAt( pos ) >= 66 && info.src.charCodeAt( pos ) <= 77 ) || ( info.src.charCodeAt( pos ) >= 80 && info.src.charCodeAt( pos ) <= 254 ) ) state = 1;
else if( info.src.charCodeAt( pos ) == 9 ) state = 2;
else if( info.src.charCodeAt( pos ) == 40 ) state = 3;
else if( info.src.charCodeAt( pos ) == 41 ) state = 4;
else if( info.src.charCodeAt( pos ) == 60 || info.src.charCodeAt( pos ) == 62 ) state = 5;
else if( info.src.charCodeAt( pos ) == 34 ) state = 11;
else if( info.src.charCodeAt( pos ) == 33 ) state = 11;
else if( info.src.charCodeAt( pos ) == 79 ) state = 12;
else if( info.src.charCodeAt( pos ) == 32 ) state = 13;
else if( info.src.charCodeAt( pos ) == 61 ) state = 14;
else if( info.src.charCodeAt( pos ) == 65 ) state = 18;
else if( info.src.charCodeAt( pos ) == 78 ) state = 19;
else if( info.src.charCodeAt( pos ) == 34 ) state = 15;
else if( info.src.charCodeAt( pos ) == 65 ) state = 19;
else if( info.src.charCodeAt( pos ) == 78 ) state = 20;
else state = -1;
break;
......@@ -183,9 +184,7 @@ switch( state )
break;
case 11:
if( info.src.charCodeAt( pos ) == 34 ) state = 7;
else if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 33 ) || ( info.src.charCodeAt( pos ) >= 35 && info.src.charCodeAt( pos ) <= 91 ) || ( info.src.charCodeAt( pos ) >= 93 && info.src.charCodeAt( pos ) <= 254 ) ) state = 11;
else if( info.src.charCodeAt( pos ) == 92 ) state = 15;
if( info.src.charCodeAt( pos ) == 61 ) state = 14;
else state = -1;
break;
......@@ -211,7 +210,9 @@ switch( state )
break;
case 15:
if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 254 ) ) state = 11;
if( info.src.charCodeAt( pos ) == 34 ) state = 7;
else if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 33 ) || ( info.src.charCodeAt( pos ) >= 35 && info.src.charCodeAt( pos ) <= 91 ) || ( info.src.charCodeAt( pos ) >= 93 && info.src.charCodeAt( pos ) <= 254 ) ) state = 15;
else if( info.src.charCodeAt( pos ) == 92 ) state = 17;
else state = -1;
break;
......@@ -225,6 +226,11 @@ switch( state )
break;
case 17:
if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 254 ) ) state = 15;
else state = -1;
break;
case 18:
if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 31 ) || info.src.charCodeAt( pos ) == 33 || ( info.src.charCodeAt( pos ) >= 35 && info.src.charCodeAt( pos ) <= 39 ) || ( info.src.charCodeAt( pos ) >= 42 && info.src.charCodeAt( pos ) <= 57 ) || ( info.src.charCodeAt( pos ) >= 59 && info.src.charCodeAt( pos ) <= 83 ) || ( info.src.charCodeAt( pos ) >= 85 && info.src.charCodeAt( pos ) <= 254 ) ) state = 1;
else if( info.src.charCodeAt( pos ) == 58 ) state = 6;
else if( info.src.charCodeAt( pos ) == 84 ) state = 10;
......@@ -233,7 +239,7 @@ switch( state )
match_pos = pos;
break;
case 18:
case 19:
if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 31 ) || info.src.charCodeAt( pos ) == 33 || ( info.src.charCodeAt( pos ) >= 35 && info.src.charCodeAt( pos ) <= 39 ) || ( info.src.charCodeAt( pos ) >= 42 && info.src.charCodeAt( pos ) <= 57 ) || ( info.src.charCodeAt( pos ) >= 59 && info.src.charCodeAt( pos ) <= 77 ) || ( info.src.charCodeAt( pos ) >= 79 && info.src.charCodeAt( pos ) <= 254 ) ) state = 1;
else if( info.src.charCodeAt( pos ) == 58 ) state = 6;
else if( info.src.charCodeAt( pos ) == 78 ) state = 16;
......@@ -242,10 +248,10 @@ switch( state )
match_pos = pos;
break;
case 19:
case 20:
if( ( info.src.charCodeAt( pos ) >= 0 && info.src.charCodeAt( pos ) <= 31 ) || info.src.charCodeAt( pos ) == 33 || ( info.src.charCodeAt( pos ) >= 35 && info.src.charCodeAt( pos ) <= 39 ) || ( info.src.charCodeAt( pos ) >= 42 && info.src.charCodeAt( pos ) <= 57 ) || ( info.src.charCodeAt( pos ) >= 59 && info.src.charCodeAt( pos ) <= 78 ) || ( info.src.charCodeAt( pos ) >= 80 && info.src.charCodeAt( pos ) <= 254 ) ) state = 1;
else if( info.src.charCodeAt( pos ) == 58 ) state = 6;
else if( info.src.charCodeAt( pos ) == 79 ) state = 17;
else if( info.src.charCodeAt( pos ) == 79 ) state = 18;
else state = -1;
match = 10;
match_pos = pos;
......@@ -755,7 +761,7 @@ var query_class_dict = {};
* @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare
*/
function ComplexQuery(spec) {
function ComplexQuery(spec, key_schema) {
Query.call(this);
/**
......@@ -777,7 +783,12 @@ function ComplexQuery(spec) {
* @optional
*/
this.query_list = spec.query_list || [];
this.query_list = this.query_list.map(QueryFactory.create);
/*jslint unparam: true*/
this.query_list = this.query_list.map(
// decorate the map to avoid sending the index as key_schema argument
function (o, i) { return QueryFactory.create(o, key_schema); }
);
/*jslint unparam: false*/
}
inherits(ComplexQuery, Query);
......@@ -1158,7 +1169,7 @@ function QueryFactory() {
* of a Query
* @return {Query} A Query object
*/
QueryFactory.create = function (object) {
QueryFactory.create = function (object, key_schema) {
if (object === "") {
return new Query();
}
......@@ -1167,7 +1178,7 @@ QueryFactory.create = function (object) {
}
if (typeof (object || {}).type === "string" &&
query_class_dict[object.type]) {
return new query_class_dict[object.type](object);
return new query_class_dict[object.type](object, key_schema);
}
throw new TypeError("QueryFactory.create(): " +
"Argument 1 is not a search text or a parsable object");
......@@ -1202,6 +1213,36 @@ _export("objectToSearchText", objectToSearchText);
/*global Query: true, inherits: true, query_class_dict: true, _export: true,
convertStringToRegExp, RSVP */
var checkKeySchema = function (key_schema) {
var prop;
if (key_schema !== undefined) {
if (typeof key_schema !== 'object') {
throw new TypeError("SimpleQuery().create(): " +
"key_schema is not of type 'object'");
}
// key_set is mandatory
if (key_schema.key_set === undefined) {
throw new TypeError("SimpleQuery().create(): " +
"key_schema has no 'key_set' property");
}
for (prop in key_schema) {
if (key_schema.hasOwnProperty(prop)) {
switch (prop) {
case 'key_set':
case 'cast_lookup':
case 'match_lookup':
break;
default:
throw new TypeError("SimpleQuery().create(): " +
"key_schema has unknown property '" + prop + "'");
}
}
}
}
};
/**
* The SimpleQuery inherits from Query, and compares one metadata value
*
......@@ -1212,9 +1253,13 @@ _export("objectToSearchText", objectToSearchText);
* @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare
*/
function SimpleQuery(spec) {
function SimpleQuery(spec, key_schema) {
Query.call(this);
checkKeySchema(key_schema);
this._key_schema = key_schema || {};
/**
* Operator to use to compare object values
*
......@@ -1244,11 +1289,81 @@ function SimpleQuery(spec) {
}
inherits(SimpleQuery, Query);
var checkKey = function (key) {
var prop;
if (key.read_from === undefined) {
throw new TypeError("Custom key is missing the read_from property");
}
for (prop in key) {
if (key.hasOwnProperty(prop)) {
switch (prop) {
case 'read_from':
case 'cast_to':
case 'equal_match':
break;
default:
throw new TypeError("Custom key has unknown property '" +
prop + "'");
}
}
}
};
/**
* #crossLink "Query/match:method"
*/
SimpleQuery.prototype.match = function (item, wildcard_character) {
return this[this.operator](item[this.key], this.value, wildcard_character);
var object_value = null,
equal_match = null,
cast_to = null,
matchMethod = null,
value = null,
key = this.key;
matchMethod = this[this.operator];
if (this._key_schema.key_set && this._key_schema.key_set[key] !== undefined) {
key = this._key_schema.key_set[key];
}
if (typeof key === 'object') {
checkKey(key);
object_value = item[key.read_from];
equal_match = key.equal_match;
// equal_match can be a string
if (typeof equal_match === 'string') {
// XXX raise error if equal_match not in match_lookup
equal_match = this._key_schema.match_lookup[equal_match];
}
// equal_match overrides the default '=' operator
if (equal_match !== undefined) {
matchMethod = (this.operator === '=') ? equal_match : matchMethod;
}
value = this.value;
cast_to = key.cast_to;
if (cast_to) {
// cast_to can be a string
if (typeof cast_to === 'string') {
// XXX raise error if cast_to not in cast_lookup
cast_to = this._key_schema.cast_lookup[cast_to];
}
value = cast_to(value);
object_value = cast_to(object_value);
}
} else {
object_value = item[key];
value = this.value;
}
return matchMethod(object_value, value, wildcard_character);
};
/**
......@@ -1288,7 +1403,7 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value,
}
for (i = 0; i < object_value.length; i += 1) {
value = object_value[i];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (comparison_value === undefined) {
......@@ -1300,6 +1415,10 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value,
if (value === undefined) {
return RSVP.resolve(false);
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value,
wildcard_character) === 0);
}
if (
convertStringToRegExp(
comparison_value.toString(),
......@@ -1329,7 +1448,7 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
}
for (i = 0; i < object_value.length; i += 1) {
value = object_value[i];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (comparison_value === undefined) {
......@@ -1341,6 +1460,10 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
if (value === undefined) {
return RSVP.resolve(true);
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value,
wildcard_character) !== 0);
}
if (
convertStringToRegExp(
comparison_value.toString(),
......@@ -1367,9 +1490,15 @@ SimpleQuery.prototype["<"] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value === undefined || comparison_value === undefined) {
return RSVP.resolve(false);
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) < 0);
}
return RSVP.resolve(value < comparison_value);
};
......@@ -1388,9 +1517,15 @@ SimpleQuery.prototype["<="] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value === undefined || comparison_value === undefined) {
return RSVP.resolve(false);
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) <= 0);
}
return RSVP.resolve(value <= comparison_value);
};
......@@ -1409,9 +1544,15 @@ SimpleQuery.prototype[">"] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value === undefined || comparison_value === undefined) {
return RSVP.resolve(false);
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) > 0);
}
return RSVP.resolve(value > comparison_value);
};
......@@ -1430,9 +1571,15 @@ SimpleQuery.prototype[">="] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value === undefined || comparison_value === undefined) {
return RSVP.resolve(false);
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) >= 0);
}
return RSVP.resolve(value >= comparison_value);
};
......
_build/*
env
# Makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build
# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
clean:
-rm -rf $(BUILDDIR)/*
html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."
json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."
htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."
qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/JIO.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/JIO.qhc"
devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/JIO"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/JIO"
@echo "# devhelp"
epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."
latexpdf:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."
man:
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
texinfo:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."
info:
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
gettext:
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."
linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."
doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."
/* override sphinx theme */
.line-block {
margin-bottom: 0.4em;
}
pre {
padding-left: 15px;
padding-righ: 15px;
font-size: 0.8em;
}
a.button {
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
display:inline-block;
color:#ffffff;
font-family:arial;
font-size:15px;
font-weight:bold;
padding:6px 24px;
text-decoration:none;
}
a.button:hover {
color:#ffffff;
}
a.button:active {
position:relative;
top:1px;
}
a.blue-button {
-moz-box-shadow:inset 0px 1px 0px 0px #97c4fe;
-webkit-box-shadow:inset 0px 1px 0px 0px #97c4fe;
box-shadow:inset 0px 1px 0px 0px #97c4fe;
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #3d94f6), color-stop(1, #1e62d0));
background:-moz-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:-webkit-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:-o-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:-ms-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:linear-gradient(to bottom, #3d94f6 5%, #1e62d0 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#3d94f6', endColorstr='#1e62d0',GradientType=0);
background-color:#3d94f6;
border:1px solid #337fed;
text-shadow:0px 1px 0px #1570cd;
}
a.blue-button:hover {
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #1e62d0), color-stop(1, #3d94f6));
background:-moz-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:-webkit-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:-o-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:-ms-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:linear-gradient(to bottom, #1e62d0 5%, #3d94f6 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#1e62d0', endColorstr='#3d94f6',GradientType=0);
background-color:#1e62d0;
}
a.green-button {
-moz-box-shadow:inset 0px 1px 0px 0px #9acc85;
-webkit-box-shadow:inset 0px 1px 0px 0px #9acc85;
box-shadow:inset 0px 1px 0px 0px #9acc85;
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #74ad5a), color-stop(1, #68a54b));
background:-moz-linear-gradient(top, #74ad5a 5%, #68a54b 100%);
background:-webkit-linear-gradient(top, #74ad5a 5%, #68a54b 100%);
background:-o-linear-gradient(top, #74ad5a 5%, #68a54b 100%);
background:-ms-linear-gradient(top, #74ad5a 5%, #68a54b 100%);
background:linear-gradient(to bottom, #74ad5a 5%, #68a54b 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#74ad5a', endColorstr='#68a54b',GradientType=0);
background-color:#74ad5a;
border:1px solid #3b6e22;
text-shadow:0px 1px 0px #92b879;
}
a.green-button:hover {
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #68a54b), color-stop(1, #74ad5a));
background:-moz-linear-gradient(top, #68a54b 5%, #74ad5a 100%);
background:-webkit-linear-gradient(top, #68a54b 5%, #74ad5a 100%);
background:-o-linear-gradient(top, #68a54b 5%, #74ad5a 100%);
background:-ms-linear-gradient(top, #68a54b 5%, #74ad5a 100%);
background:linear-gradient(to bottom, #68a54b 5%, #74ad5a 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#68a54b', endColorstr='#74ad5a',GradientType=0);
background-color:#68a54b;
}
/* jslint undef: true */
/* global jQuery, document */
jQuery(document).ready(function ($) {
$('a:contains(Full download)').addClass('button').addClass('blue-button').addClass('blue');
$('a:contains(Minified download)').addClass('button').addClass('green-button').addClass('blue');
});
{%- extends "basic/layout.html" %}
{%- block extrahead %}
<link rel="stylesheet" type="text/css" href="_static/jiodocs.css">
<script type="text/javascript" src="_static/jiodocs.js"></script>
{{ super() }}
{% if theme_touch_icon %}
<link rel="apple-touch-icon" href="{{ pathto('_static/' ~ theme_touch_icon, 1) }}" />
{% endif %}
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
{% endblock %}
{%- block relbar2 %}{% endblock %}
{%- block footer %}
<div class="footer">
&copy; Copyright {{ copyright }}.
<a href="https://github.com/nexedi/jio" class="github">
<img style="position: absolute; top: 0; right: 0; border: 0;"
src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png"
alt="Fork me on GitHub">
</a>
{%- endblock %}
<h4><a href="./getting_started.html#download-fork" style="color:#008800">Downloads</a></h4>
<h4><a href="./getting_started.html">Getting Started</a></h4>
<h3>Stay Informed</h3>
You can follow our <a href="http://www.j-io.org/blog">blog</a> to get notified about new releases.
<h3>Support</h3>
<p>
We have a <a href="http://www.j-io.org/forum">forum</a> and an <a href="https://github.com/nexedi/jio/issues">issue tracker</a>
on GitHub.
</p>
<p>
<iframe src="http://ghbtns.com/github-btn.html?user=nexedi&repo=jio&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="200px" height="35px"></iframe>
</p>
*.pyc
*.pyo
.DS_Store
Modifications:
Copyright (c) 2010 Kenneth Reitz.
Original Project:
Copyright (c) 2010 by Armin Ronacher.
Some rights reserved.
Redistribution and use in source and binary forms of the theme, with or
without modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* The names of the contributors may not be used to endorse or
promote products derived from this software without specific
prior written permission.
We kindly ask you to only use these themes in an unmodified manner just
for Flask and Flask-related products, not for unrelated projects. If you
like the visual style and want to use it for your own projects, please
consider making some larger changes to the themes (such as changing
font faces, sizes, colors or margins).
THIS THEME IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS THEME, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
# flasky extensions. flasky pygments style based on tango style
from pygments.style import Style
from pygments.token import Keyword, Name, Comment, String, Error, \
Number, Operator, Generic, Whitespace, Punctuation, Other, Literal
class FlaskyStyle(Style):
background_color = "#f8f8f8"
default_style = ""
styles = {
# No corresponding class for the following:
#Text: "", # class: ''
Whitespace: "underline #f8f8f8", # class: 'w'
Error: "#a40000 border:#ef2929", # class: 'err'
Other: "#000000", # class 'x'
Comment: "italic #8f5902", # class: 'c'
Comment.Preproc: "noitalic", # class: 'cp'
Keyword: "bold #004461", # class: 'k'
Keyword.Constant: "bold #004461", # class: 'kc'
Keyword.Declaration: "bold #004461", # class: 'kd'
Keyword.Namespace: "bold #004461", # class: 'kn'
Keyword.Pseudo: "bold #004461", # class: 'kp'
Keyword.Reserved: "bold #004461", # class: 'kr'
Keyword.Type: "bold #004461", # class: 'kt'
Operator: "#582800", # class: 'o'
Operator.Word: "bold #004461", # class: 'ow' - like keywords
Punctuation: "bold #000000", # class: 'p'
# because special names such as Name.Class, Name.Function, etc.
# are not recognized as such later in the parsing, we choose them
# to look the same as ordinary variables.
Name: "#000000", # class: 'n'
Name.Attribute: "#c4a000", # class: 'na' - to be revised
Name.Builtin: "#004461", # class: 'nb'
Name.Builtin.Pseudo: "#3465a4", # class: 'bp'
Name.Class: "#000000", # class: 'nc' - to be revised
Name.Constant: "#000000", # class: 'no' - to be revised
Name.Decorator: "#888", # class: 'nd' - to be revised
Name.Entity: "#ce5c00", # class: 'ni'
Name.Exception: "bold #cc0000", # class: 'ne'
Name.Function: "#000000", # class: 'nf'
Name.Property: "#000000", # class: 'py'
Name.Label: "#f57900", # class: 'nl'
Name.Namespace: "#000000", # class: 'nn' - to be revised
Name.Other: "#000000", # class: 'nx'
Name.Tag: "bold #004461", # class: 'nt' - like a keyword
Name.Variable: "#000000", # class: 'nv' - to be revised
Name.Variable.Class: "#000000", # class: 'vc' - to be revised
Name.Variable.Global: "#000000", # class: 'vg' - to be revised
Name.Variable.Instance: "#000000", # class: 'vi' - to be revised
Number: "#990000", # class: 'm'
Literal: "#000000", # class: 'l'
Literal.Date: "#000000", # class: 'ld'
String: "#4e9a06", # class: 's'
String.Backtick: "#4e9a06", # class: 'sb'
String.Char: "#4e9a06", # class: 'sc'
String.Doc: "italic #8f5902", # class: 'sd' - like a comment
String.Double: "#4e9a06", # class: 's2'
String.Escape: "#4e9a06", # class: 'se'
String.Heredoc: "#4e9a06", # class: 'sh'
String.Interpol: "#4e9a06", # class: 'si'
String.Other: "#4e9a06", # class: 'sx'
String.Regex: "#4e9a06", # class: 'sr'
String.Single: "#4e9a06", # class: 's1'
String.Symbol: "#4e9a06", # class: 'ss'
Generic: "#000000", # class: 'g'
Generic.Deleted: "#a40000", # class: 'gd'
Generic.Emph: "italic #000000", # class: 'ge'
Generic.Error: "#ef2929", # class: 'gr'
Generic.Heading: "bold #000080", # class: 'gh'
Generic.Inserted: "#00A000", # class: 'gi'
Generic.Output: "#888", # class: 'go'
Generic.Prompt: "#745334", # class: 'gp'
Generic.Strong: "bold #000000", # class: 'gs'
Generic.Subheading: "bold #800080", # class: 'gu'
Generic.Traceback: "bold #a40000", # class: 'gt'
}
{%- extends "basic/layout.html" %}
{%- block extrahead %}
{{ super() }}
{% if theme_touch_icon %}
<link rel="apple-touch-icon" href="{{ pathto('_static/' ~ theme_touch_icon, 1) }}" />
{% endif %}
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
{% endblock %}
{%- block relbar2 %}{% endblock %}
{%- block footer %}
<div class="footer">
&copy; Copyright {{ copyright }}.
</div>
<a href="https://github.com/nexedi/jio" class="github">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" />
</a>
<script type="text/javascript">
var _gauges = _gauges || [];
(function() {
var t = document.createElement('script');
t.type = 'text/javascript';
t.async = true;
t.id = 'gauges-tracker';
t.setAttribute('data-site-id', '50f10bd6613f5d3e2c00000e');
t.src = '//secure.gaug.es/track.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(t, s);
})();
</script>
{%- endblock %}
<h3>Related Topics</h3>
<ul>
<li><a href="{{ pathto(master_doc) }}">Documentation overview</a><ul>
{%- for parent in parents %}
<li><a href="{{ parent.link|e }}">{{ parent.title }}</a><ul>
{%- endfor %}
{%- if prev %}
<li>Previous: <a href="{{ prev.link|e }}" title="{{ _('previous chapter')
}}">{{ prev.title }}</a></li>
{%- endif %}
{%- if next %}
<li>Next: <a href="{{ next.link|e }}" title="{{ _('next chapter')
}}">{{ next.title }}</a></li>
{%- endif %}
{%- for parent in parents %}
</ul></li>
{%- endfor %}
</ul></li>
</ul>
/*
* flasky.css_t
* ~~~~~~~~~~~~
*
* :copyright: Copyright 2010 by Armin Ronacher. Modifications by Kenneth Reitz.
* :license: Flask Design License, see LICENSE for details.
*/
{% set page_width = '940px' %}
{% set sidebar_width = '220px' %}
@import url("basic.css");
/* -- page layout ----------------------------------------------------------- */
body {
font-family: 'goudy old style', 'minion pro', 'bell mt', Georgia, 'Hiragino Mincho Pro';
font-size: 17px;
background-color: white;
color: #000;
margin: 0;
padding: 0;
}
div.document {
width: {{ page_width }};
margin: 30px auto 0 auto;
}
div.documentwrapper {
float: left;
width: 100%;
}
div.bodywrapper {
margin: 0 0 0 {{ sidebar_width }};
}
div.sphinxsidebar {
width: {{ sidebar_width }};
}
hr {
border: 1px solid #B1B4B6;
}
div.body {
background-color: #ffffff;
color: #3E4349;
padding: 0 30px 0 30px;
}
img.floatingflask {
padding: 0 0 10px 10px;
float: right;
}
div.footer {
width: {{ page_width }};
margin: 20px auto 30px auto;
font-size: 14px;
color: #888;
text-align: right;
}
div.footer a {
color: #888;
}
div.related {
display: none;
}
div.sphinxsidebar a {
color: #444;
text-decoration: none;
border-bottom: 1px dotted #999;
}
div.sphinxsidebar a:hover {
border-bottom: 1px solid #999;
}
div.sphinxsidebar {
font-size: 14px;
line-height: 1.5;
}
div.sphinxsidebarwrapper {
padding: 18px 10px;
}
div.sphinxsidebarwrapper p.logo {
padding: 0;
margin: -10px 0 0 -20px;
text-align: center;
}
div.sphinxsidebarwrapper h1 {
font-size: 25px;
}
div.sphinxsidebar h3,
div.sphinxsidebar h4 {
font-family: 'Garamond', 'Georgia', serif;
color: #444;
font-size: 24px;
font-weight: normal;
margin: 0 0 5px 0;
padding: 0;
}
div.sphinxsidebar h4 {
font-size: 20px;
}
div.sphinxsidebar h3 a {
color: #444;
}
div.sphinxsidebar p.logo a,
div.sphinxsidebar h3 a,
div.sphinxsidebar p.logo a:hover,
div.sphinxsidebar h3 a:hover {
border: none;
}
div.sphinxsidebar p {
color: #555;
margin: 10px 0;
}
div.sphinxsidebar ul {
margin: 10px 0;
padding: 0;
color: #000;
}
div.sphinxsidebar input {
border: 1px solid #ccc;
font-family: 'Georgia', serif;
font-size: 1em;
}
/* -- body styles ----------------------------------------------------------- */
a {
color: #004B6B;
text-decoration: underline;
}
a:hover {
color: #6D4100;
text-decoration: underline;
}
div.body h1,
div.body h2,
div.body h3,
div.body h4,
div.body h5,
div.body h6 {
font-family: 'Garamond', 'Georgia', serif;
font-weight: normal;
margin: 30px 0px 10px 0px;
padding: 0;
}
div.body h1 { margin-top: 0; padding-top: 0; font-size: 240%; }
div.body h2 { font-size: 180%; }
div.body h3 { font-size: 150%; }
div.body h4 { font-size: 130%; }
div.body h5 { font-size: 100%; }
div.body h6 { font-size: 100%; }
a.headerlink {
color: #ddd;
padding: 0 4px;
text-decoration: none;
}
a.headerlink:hover {
color: #444;
background: #eaeaea;
}
div.body p, div.body dd, div.body li {
line-height: 1.4em;
}
div.admonition {
background: #fafafa;
margin: 20px -30px;
padding: 10px 30px;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
div.admonition tt.xref, div.admonition a tt {
border-bottom: 1px solid #fafafa;
}
dd div.admonition {
margin-left: -60px;
padding-left: 60px;
}
div.admonition p.admonition-title {
font-family: 'Garamond', 'Georgia', serif;
font-weight: normal;
font-size: 24px;
margin: 0 0 10px 0;
padding: 0;
line-height: 1;
}
div.admonition p.last {
margin-bottom: 0;
}
div.highlight {
background-color: white;
}
dt:target, .highlight {
background: #FAF3E8;
}
div.note {
background-color: #eee;
border: 1px solid #ccc;
}
div.seealso {
background-color: #ffc;
border: 1px solid #ff6;
}
div.topic {
background-color: #eee;
}
p.admonition-title {
display: inline;
}
p.admonition-title:after {
content: ":";
}
pre, tt {
font-family: 'Consolas', 'Menlo', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace;
font-size: 0.9em;
}
img.screenshot {
}
tt.descname, tt.descclassname {
font-size: 0.95em;
}
tt.descname {
padding-right: 0.08em;
}
img.screenshot {
-moz-box-shadow: 2px 2px 4px #eee;
-webkit-box-shadow: 2px 2px 4px #eee;
box-shadow: 2px 2px 4px #eee;
}
table.docutils {
border: 1px solid #888;
-moz-box-shadow: 2px 2px 4px #eee;
-webkit-box-shadow: 2px 2px 4px #eee;
box-shadow: 2px 2px 4px #eee;
}
table.docutils td, table.docutils th {
border: 1px solid #888;
padding: 0.25em 0.7em;
}
table.field-list, table.footnote {
border: none;
-moz-box-shadow: none;
-webkit-box-shadow: none;
box-shadow: none;
}
table.footnote {
margin: 15px 0;
width: 100%;
border: 1px solid #eee;
background: #fdfdfd;
font-size: 0.9em;
}
table.footnote + table.footnote {
margin-top: -15px;
border-top: none;
}
table.field-list th {
padding: 0 0.8em 0 0;
}
table.field-list td {
padding: 0;
}
table.footnote td.label {
width: 0px;
padding: 0.3em 0 0.3em 0.5em;
}
table.footnote td {
padding: 0.3em 0.5em;
}
dl {
margin: 0;
padding: 0;
}
dl dd {
margin-left: 30px;
}
blockquote {
margin: 0 0 0 30px;
padding: 0;
}
ul, ol {
margin: 10px 0 10px 30px;
padding: 0;
}
pre {
background: #eee;
padding: 7px 30px;
line-height: 1.3em;
}
dl pre, blockquote pre, li pre {
margin-left: -60px;
padding-left: 60px;
}
dl dl pre {
margin-left: -90px;
padding-left: 90px;
}
tt {
background-color: #ecf0f3;
color: #222;
/* padding: 1px 2px; */
}
tt.xref, a tt {
background-color: #FBFBFB;
border-bottom: 1px solid white;
}
a.reference {
text-decoration: none;
border-bottom: 1px dotted #004B6B;
}
a.reference:hover {
border-bottom: 1px solid #6D4100;
}
a.footnote-reference {
text-decoration: none;
font-size: 0.7em;
vertical-align: top;
border-bottom: 1px dotted #004B6B;
}
a.footnote-reference:hover {
border-bottom: 1px solid #6D4100;
}
a:hover tt {
background: #EEE;
}
@media screen and (max-width: 870px) {
div.sphinxsidebar {
display: none;
}
div.document {
width: 100%;
}
div.documentwrapper {
margin-left: 0;
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
}
div.bodywrapper {
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
}
ul {
margin-left: 0;
}
.document {
width: auto;
}
.footer {
width: auto;
}
.bodywrapper {
margin: 0;
}
.footer {
width: auto;
}
.github {
display: none;
}
}
@media screen and (max-width: 875px) {
body {
margin: 0;
padding: 20px 30px;
}
div.documentwrapper {
float: none;
background: white;
}
div.sphinxsidebar {
display: block;
float: none;
width: 102.5%;
margin: 50px -30px -20px -30px;
padding: 10px 20px;
background: #333;
color: white;
}
div.sphinxsidebar h3, div.sphinxsidebar h4, div.sphinxsidebar p,
div.sphinxsidebar h3 a {
color: white;
}
div.sphinxsidebar a {
color: #aaa;
}
div.sphinxsidebar p.logo {
display: none;
}
div.document {
width: 100%;
margin: 0;
}
div.related {
display: block;
margin: 0;
padding: 10px 0 20px 0;
}
div.related ul,
div.related ul li {
margin: 0;
padding: 0;
}
div.footer {
display: none;
}
div.bodywrapper {
margin: 0;
}
div.body {
min-height: 0;
padding: 0;
}
.rtd_doc_footer {
display: none;
}
.document {
width: auto;
}
.footer {
width: auto;
}
.footer {
width: auto;
}
.github {
display: none;
}
}
/* misc. */
.revsys-inline {
display: none!important;
}
/*
* small_flask.css_t
* ~~~~~~~~~~~~~~~~~
*
* :copyright: Copyright 2010 by Armin Ronacher.
* :license: Flask Design License, see LICENSE for details.
*/
body {
margin: 0;
padding: 20px 30px;
}
div.documentwrapper {
float: none;
background: white;
}
div.sphinxsidebar {
display: block;
float: none;
width: 102.5%;
margin: 50px -30px -20px -30px;
padding: 10px 20px;
background: #333;
color: white;
}
div.sphinxsidebar h3, div.sphinxsidebar h4, div.sphinxsidebar p,
div.sphinxsidebar h3 a {
color: white;
}
div.sphinxsidebar a {
color: #aaa;
}
div.sphinxsidebar p.logo {
display: none;
}
div.document {
width: 100%;
margin: 0;
}
div.related {
display: block;
margin: 0;
padding: 10px 0 20px 0;
}
div.related ul,
div.related ul li {
margin: 0;
padding: 0;
}
div.footer {
display: none;
}
div.bodywrapper {
margin: 0;
}
div.body {
min-height: 0;
padding: 0;
}
[theme]
inherit = basic
stylesheet = flasky.css
pygments_style = flask_theme_support.FlaskyStyle
[options]
touch_icon =
{% extends "basic/layout.html" %}
{% block header %}
{{ super() }}
{% if pagename == 'index' %}
<div class=indexwrapper>
{% endif %}
{% endblock %}
{% block footer %}
{% if pagename == 'index' %}
</div>
{% endif %}
{% endblock %}
{# do not display relbars #}
{% block relbar1 %}{% endblock %}
{% block relbar2 %}
{% if theme_github_fork %}
<a href="http://github.com/{{ theme_github_fork }}"><img style="position: fixed; top: 0; right: 0; border: 0;"
src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
{% endif %}
{% endblock %}
{% block sidebar1 %}{% endblock %}
{% block sidebar2 %}{% endblock %}
/*
* flasky.css_t
* ~~~~~~~~~~~~
*
* Sphinx stylesheet -- flasky theme based on nature theme.
*
* :copyright: Copyright 2007-2010 by the Sphinx team, see AUTHORS.
* :license: BSD, see LICENSE for details.
*
*/
@import url("basic.css");
/* -- page layout ----------------------------------------------------------- */
body {
font-family: 'Georgia', serif;
font-size: 17px;
color: #000;
background: white;
margin: 0;
padding: 0;
}
div.documentwrapper {
float: left;
width: 100%;
}
div.bodywrapper {
margin: 40px auto 0 auto;
width: 700px;
}
hr {
border: 1px solid #B1B4B6;
}
div.body {
background-color: #ffffff;
color: #3E4349;
padding: 0 30px 30px 30px;
}
img.floatingflask {
padding: 0 0 10px 10px;
float: right;
}
div.footer {
text-align: right;
color: #888;
padding: 10px;
font-size: 14px;
width: 650px;
margin: 0 auto 40px auto;
}
div.footer a {
color: #888;
text-decoration: underline;
}
div.related {
line-height: 32px;
color: #888;
}
div.related ul {
padding: 0 0 0 10px;
}
div.related a {
color: #444;
}
/* -- body styles ----------------------------------------------------------- */
a {
color: #004B6B;
text-decoration: underline;
}
a:hover {
color: #6D4100;
text-decoration: underline;
}
div.body {
padding-bottom: 40px; /* saved for footer */
}
div.body h1,
div.body h2,
div.body h3,
div.body h4,
div.body h5,
div.body h6 {
font-family: 'Garamond', 'Georgia', serif;
font-weight: normal;
margin: 30px 0px 10px 0px;
padding: 0;
}
{% if theme_index_logo %}
div.indexwrapper h1 {
text-indent: -999999px;
background: url({{ theme_index_logo }}) no-repeat center center;
height: {{ theme_index_logo_height }};
}
{% endif %}
div.body h2 { font-size: 180%; }
div.body h3 { font-size: 150%; }
div.body h4 { font-size: 130%; }
div.body h5 { font-size: 100%; }
div.body h6 { font-size: 100%; }
a.headerlink {
color: white;
padding: 0 4px;
text-decoration: none;
}
a.headerlink:hover {
color: #444;
background: #eaeaea;
}
div.body p, div.body dd, div.body li {
line-height: 1.4em;
}
div.admonition {
background: #fafafa;
margin: 20px -30px;
padding: 10px 30px;
border-top: 1px solid #ccc;
border-bottom: 1px solid #ccc;
}
div.admonition p.admonition-title {
font-family: 'Garamond', 'Georgia', serif;
font-weight: normal;
font-size: 24px;
margin: 0 0 10px 0;
padding: 0;
line-height: 1;
}
div.admonition p.last {
margin-bottom: 0;
}
div.highlight{
background-color: white;
}
dt:target, .highlight {
background: #FAF3E8;
}
div.note {
background-color: #eee;
border: 1px solid #ccc;
}
div.seealso {
background-color: #ffc;
border: 1px solid #ff6;
}
div.topic {
background-color: #eee;
}
div.warning {
background-color: #ffe4e4;
border: 1px solid #f66;
}
p.admonition-title {
display: inline;
}
p.admonition-title:after {
content: ":";
}
pre, tt {
font-family: 'Consolas', 'Menlo', 'Deja Vu Sans Mono', 'Bitstream Vera Sans Mono', monospace;
font-size: 0.85em;
}
img.screenshot {
}
tt.descname, tt.descclassname {
font-size: 0.95em;
}
tt.descname {
padding-right: 0.08em;
}
img.screenshot {
-moz-box-shadow: 2px 2px 4px #eee;
-webkit-box-shadow: 2px 2px 4px #eee;
box-shadow: 2px 2px 4px #eee;
}
table.docutils {
border: 1px solid #888;
-moz-box-shadow: 2px 2px 4px #eee;
-webkit-box-shadow: 2px 2px 4px #eee;
box-shadow: 2px 2px 4px #eee;
}
table.docutils td, table.docutils th {
border: 1px solid #888;
padding: 0.25em 0.7em;
}
table.field-list, table.footnote {
border: none;
-moz-box-shadow: none;
-webkit-box-shadow: none;
box-shadow: none;
}
table.footnote {
margin: 15px 0;
width: 100%;
border: 1px solid #eee;
}
table.field-list th {
padding: 0 0.8em 0 0;
}
table.field-list td {
padding: 0;
}
table.footnote td {
padding: 0.5em;
}
dl {
margin: 0;
padding: 0;
}
dl dd {
margin-left: 30px;
}
pre {
padding: 0;
margin: 15px -30px;
padding: 8px;
line-height: 1.3em;
padding: 7px 30px;
background: #eee;
border-radius: 2px;
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
}
dl pre {
margin-left: -60px;
padding-left: 60px;
}
tt {
background-color: #ecf0f3;
color: #222;
/* padding: 1px 2px; */
}
tt.xref, a tt {
background-color: #FBFBFB;
}
a:hover tt {
background: #EEE;
}
[theme]
inherit = basic
stylesheet = flasky.css
nosidebar = true
pygments_style = flask_theme_support.FlaskyStyle
[options]
index_logo = ''
index_logo_height = 120px
github_fork = ''
Authors
=======
* Francois Billioud
* Tristan Cavelier
* Sven Franck
.. _list-of-available-storages:
List of Available Storages
==========================
jIO saves his job queue in a workspace which is localStorage by default.
Provided storage descriptions are also stored, and it can be dangerous to
store passwords.
The best way to create a storage description is to use the (often) provided
tool given by the storage library. The returned description is secured to avoid
cleartext, readable passwords (as opposed to encrypted passwords for instance).
When building storage trees, there is no limit on the number of storages you
can use. The only thing you have to be aware of is compatibility of simple and
revision based storages.
Connectors
----------
LocalStorage
^^^^^^^^^^^^
Three methods are provided:
* ``.createDescription(username, [application_name], [mode='localStorage'])``
* ``.createLocalDescription(username, [application_name])``
* ``.createMemoryDescription(username, [application_name])``
All parameters are strings.
Examples:
.. code-block:: javascript
// to work on browser localStorage
var jio = jIO.createJIO(local_storage.createDescription('me'));
// to work on browser memory
var jio = jIO.createJIO(local_storage.createMemoryDescription('me'));
DavStorage
^^^^^^^^^^
The method ``dav_storage.createDescription()`` generates a DAV storage description for
*none*, *basic* or *digest* authentication.
NB: digest **is not implemented yet**.
.. code-block:: javascript
dav_storage.createDescription(url, auth_type,
[realm], [username], [password]);
All parameters are strings.
============= ========================
parameter required?
============= ========================
``url`` yes
``auth_type`` yes
``realm`` if auth_type == 'digest'
``username`` if auth_type != 'none'
``password`` if auth-type != 'none'
============= ========================
If ``auth_type`` is the string ``"none"``, then ``realm``, ``username`` and ``password`` are never used.
**Be careful**: The generated description never contains a readable password, but
for basic authentication, the password will just be base64 encoded.
S3Storage
^^^^^^^^^
Updating to v2.0
XWikiStorage
^^^^^^^^^^^^
Updating to v2.0
Handlers
--------
IndexStorage
^^^^^^^^^^^^
This handler indexes documents metadata into a database (which is a simple
document) to increase the speed of ``.allDocs()`` requests. However, it is not able to
manage the ``include_docs`` option.
The sub storages have to manage ``query`` and ``include_docs`` options.
Here is the description:
.. code-block:: javascript
{
type: 'index',
indices: [{
// doc id where to store indices
id: 'index_title_subject.json',
// metadata to index
index: ['title', 'subject'],
attachment: 'db.json', // default 'body'
// additional metadata to add to database, default undefined
metadata: {
type: 'Dataset',
format: 'application/json',
title: 'My index database',
creator: 'Me'
},
// default equal to parent sub_storage field
sub_storage: <sub storage where to store index>
}, {
id: 'index_year.json',
index: 'year'
...
}],
sub_storage: <sub storage description>
}
GIDStorage
^^^^^^^^^^
:ref:`Full description here <gid-storage>`.
Updating to v2.0
SplitStorage
^^^^^^^^^^^^
Updating to v2.0
Replicate Storage
^^^^^^^^^^^^^^^^^
Coming soon
Revision Based Handlers
-----------------------
A revision based handler is a storage which is able to do some document
versioning using simple storages listed above.
On jIO command parameter, ``_id`` is still used to identify a document, but
another id ``_rev`` must be defined to use a specific revision of that document.
On command responses, you will find another field ``rev`` which will represent the
new revision produced by your action. All the document history is kept unless
you decide to delete older revisions.
Other fields ``conflicts``, ``revisions`` and ``revs_info`` can be returned if the
options **conflicts: true**, **revs: true** or **revs_info: true** are set.
Revision Storage
^^^^^^^^^^^^^^^^
Updating to v2.0
Replicate Revision Storage
^^^^^^^^^^^^^^^^^^^^^^^^^^
Updating to v2.0
jIO Complex Queries
===================
What are Complex Queries?
-------------------------
In jIO, a complex query can ask a storage server to select, filter, sort, or
limit a document list before sending it back. If the server is not able to do
so, the complex query tool can do the filtering by itself on the client. Only the
``.allDocs()`` method can use complex queries.
A query can either be a string (using a specific language useful for writing
queries), or it can be a tree of objects (useful to browse queries). To handle
complex queries, jIO uses a parsed grammar file which is compiled using `JSCC <http://jscc.phorward-software.com/>`_.
Why use Complex Queries?
------------------------
Complex queries can be used like database queries, for tasks such as:
* search a specific document
* sort a list of documents in a certain order
* avoid retrieving a list of ten thousand documents
* limit the list to show only N documents per page
For some storages (like localStorage), complex queries can be a powerful tool
to query accessible documents. When querying documents on a distant storage,
some server-side logic should be run to avoid returning too many documents
to the client. If distant storages are static, an alternative would be to use
an indexStorage with appropriate indices as complex queries will always try
to run the query on the index before querying documents itself.
How to use Complex Queries with jIO?
------------------------------------
Complex queries can be triggered by including the option named **query** in the ``.allDocs()`` method call.
Example:
.. code-block:: javascript
var options = {};
// search text query
options.query = '(creator:"John Doe") AND (format:"pdf")';
// OR query tree
options.query = {
type: 'complex',
operator: 'AND',
query_list: [{
type: 'simple',
key: 'creator',
value: 'John Doe'
}, {
type: 'simple',
key: 'format',
value: 'pdf'
}]
};
// FULL example using filtering criteria
options = {
query: '(creator:"% Doe") AND (format:"pdf")',
limit: [0, 100],
sort_on: [
['last_modified', 'descending'],
['creation_date', 'descending']
],
select_list: ['title'],
wildcard_character: '%'
};
// execution
jio_instance.allDocs(options, callback);
How to use Complex Queries outside jIO?
---------------------------------------
.. XXX 404 page missing on complex_example.html
Complex Queries provides an API - which namespace is complex_queries.
Refer to the `Complex Queries sample page <http://git.erp5.org/gitweb/jio.git/blob/HEAD:/examples/complex_example.html?js=1>`_
for how to use these methods, in and outside jIO. The module provides:
.. code-block:: javascript
{
parseStringToObject: [Function: parseStringToObject],
stringEscapeRegexpCharacters: [Function: stringEscapeRegexpCharacters],
select: [Function: select],
sortOn: [Function: sortOn],
limit: [Function: limit],
convertStringToRegExp: [Function: convertStringToRegExp],
QueryFactory: { [Function: QueryFactory] create: [Function] },
Query: [Function: Query],
SimpleQuery: {
[Function: SimpleQuery] super_: [Function: Query]
},
ComplexQuery: {
[Function: ComplexQuery] super_: [Function: Query]
}
}
(Reference API coming soon.)
Basic example:
.. code-block:: javascript
// object list (generated from documents in storage or index)
var object_list = [
{"title": "Document number 1", "creator": "John Doe"},
{"title": "Document number 2", "creator": "James Bond"}
];
// the query to run
var query = 'title: "Document number 1"';
// running the query
var result = complex_queries.QueryFactory.create(query).exec(object_list);
// console.log(result);
// [ { "title": "Document number 1", "creator": "John Doe"} ]
Other example:
.. code-block:: javascript
var result = complex_queries.QueryFactory.create(query).exec(
object_list,
{
"select": ['title', 'year'],
"limit": [20, 20], // from 20th to 40th document
"sort_on": [['title', 'ascending'], ['year', 'descending']],
"other_keys_and_values": "are_ignored"
}
);
// this case is equal to:
var result = complex_queries.QueryFactory.
create(query).exec(object_list);
complex_queries.sortOn([
['title', 'ascending'],
['year', 'descending']
], result);
complex_queries.limit([20, 20], result);
complex_queries.select(['title', 'year'], result);
Complex Queries in storage connectors
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The query exec method must only be used if the server is not able to pre-select
documents. As mentioned before, you could use an indexStorage to maintain
indices with key information on all documents in a storage. This index file
will then be used to run queries, if all of the fields required in the query answer
are available in the index.
Matching properties
^^^^^^^^^^^^^^^^^^^
Complex Queries select items which exactly match the value given in the
query. You can use wildcards ('%' is the default wildcard character), and you
can change the wildcard character in the query options object. If you don't
want to use a wildcard, just set the wildcard character to an empty string.
.. code-block:: javascript
var query = {
query: 'creator:"* Doe"',
wildcard_character: '*'
};
Should default search types be defined in jIO or in user interface components?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Default search types should be defined in the application's user interface
components because criteria like filters will be changed frequently by the
component (change ``limit: [0, 10]`` to ``limit: [10, 10]`` or ``sort_on: [['title',
'ascending']]`` to ``sort_on: [['creator', 'ascending']]``) and each component must
have its own default properties to keep their own behavior.
Convert Complex Queries into another type
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Example, convert Query object into a human readable string:
.. code-block:: javascript
var query = complex_queries.QueryFactory.
create('year: < 2000 OR title: "*a"'),
option = {
wildcard_character: '*',
limit: [0, 10]
},
human_read = {
"<": "is lower than ",
"<=": "is lower or equal than ",
">": "is greater than ",
">=": "is greater or equal than ",
"=": "matches ",
"!=": "doesn't match "
};
query.onParseStart = function (object, option) {
object.start = "The wildcard character is '" +
(option.wildcard_character || "%") +
"' and we need only the " +
option.limit[1] +
" elements from the number " +
option.limit[0] + ". ";
};
query.onParseSimpleQuery = function (object, option) {
object.parsed = object.parsed.key +
" " + human_read[object.parsed.operator] +
object.parsed.value;
};
query.onParseComplexQuery = function (object, option) {
object.parsed = "I want all document where " +
object.parsed.query_list.join(" " +
object.parsed.operator.toLowerCase() +
" ") +
". ";
};
query.onParseEnd = function (object, option) {
object.parsed = object.start + object.parsed + "Thank you!";
};
console.log(query.parse(option));
// logged: "The wildcard character is '*' and we need
// only the 10 elements from the number 0. I want all
// document where year is lower than 2000 or title
// matches *a. Thank you!"
JSON Schemas and Grammar
------------------------
Below you can find schemas for constructing queries.
* Complex Queries JSON Schema:
.. code-block:: javascript
{
"id": "ComplexQuery",
"properties": {
"type": {
"type": "string",
"format": "complex",
"default": "complex",
"description": "Type is used to recognize the query type"
},
"operator": {
"type": "string",
"format": "(AND|OR|NOT)",
"required": true,
"description": "Can be 'AND', 'OR' or 'NOT'."
},
"query_list": {
"type": "array",
"items": {
"type": "object"
},
"required": true,
"default": [],
"description": "query_list is a list of queries which " +
"can be in serialized format " +
"or in object format."
}
}
}
* Simple Queries JSON Schema:
.. code-block:: javascript
{
"id": "SimpleQuery",
"properties": {
"type": {
"type": "string",
"format": "simple",
"default": "simple",
"description": "Type is used to recognize the query type."
},
"operator": {
"type": "string",
"default": "=",
"format": "(>=?|<=?|!?=)",
"description": "The operator used to compare."
},
"id": {
"type": "string",
"default": "",
"description": "The column id."
},
"value": {
"type": "string",
"default": "",
"description": "The value we want to search."
}
}
}
* Complex Queries Grammar::
search_text
: and_expression
| and_expression search_text
| and_expression OR search_text
and_expression
: boolean_expression
| boolean_expression AND and_expression
boolean_expression
: NOT expression
| expression
expression
: ( search_text )
| COLUMN expression
| value
value
: OPERATOR string
| string
string
: WORD
| STRING
terminal:
OR -> /OR[ ]/
AND -> /AND[ ]/
NOT -> /NOT[ ]/
COLUMN -> /[^><!= :\(\)"][^ :\(\)"]*:/
STRING -> /"(\\.|[^\\"])*"/
WORD -> /[^><!= :\(\)"][^ :\(\)"]*/
OPERATOR -> /(>=?|<=?|!?=)/
LEFT_PARENTHESE -> /\(/
RIGHT_PARENTHESE -> /\)/
ignore: " "
# -*- coding: utf-8 -*-
#
# jIO documentation build configuration file, created by
# sphinx-quickstart on Fri Nov 15 11:55:08 2013.
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
#highlight_language = 'javascript'
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(0, os.path.abspath('.'))
# -- General configuration -----------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sphinx.ext.todo', 'sphinx.ext.ifconfig'] # , 'sphinxcontrib.jsdemo']
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
#source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'jIO'
copyright = u'2013, Nexedi'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '2.0.0-wip'
# The full version, including alpha/beta/rc tags.
release = '2.0.0-wip'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']
# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'default'
# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []
# -- Options for HTML output ---------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
html_theme = 'default'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
html_logo = 'jio-logo.png'
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
html_sidebars = {
'index': ['side-top.html', 'side-gettingstarted.html', 'side-downloads.html', 'side-support.html', 'side-informed.html', 'searchbox.html'],
'**': ['side-top.html', 'localtoc.html', 'relations.html', 'searchbox.html']
}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# If false, no module index is generated.
#html_domain_indices = True
# If false, no index is generated.
#html_use_index = True
# If true, the index is split into individual pages for each letter.
#html_split_index = False
# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = None
# Output file base name for HTML help builder.
htmlhelp_basename = 'jIOdoc'
# -- Options for LaTeX output --------------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#'preamble': '',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'jIO.tex', u'jIO Documentation',
u'Nexedi', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# If true, show page references after internal links.
#latex_show_pagerefs = False
# If true, show URL addresses after external links.
#latex_show_urls = False
# Documents to append as an appendix to all manuals.
#latex_appendices = []
# If false, no module index is generated.
#latex_domain_indices = True
# -- Options for manual page output --------------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'jio', u'jIO Documentation',
[u'Nexedi'], 1)
]
# If true, show URL addresses after external links.
#man_show_urls = False
# -- Options for Texinfo output ------------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'jIO', u'jIO Documentation',
u'Nexedi', 'jIO', 'A JavaScript library to manage documents across multiple storages.',
'Miscellaneous'),
]
# Documents to append as an appendix to all manuals.
#texinfo_appendices = []
# If false, no module index is generated.
#texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
#texinfo_show_urls = 'footnote'
sys.path.append(os.path.abspath('_themes'))
html_theme_path = ['_themes']
html_theme = 'kr'
For developers
==============
Quick start
-----------
The source repository includes ready-to-use files, so in case you do
not want to build jIO yourself, just use *jio.js* as well as *complex_queries.js*
plus the storages and dependencies you need and you will be good to go.
If you want to modify or build jIO yourself, you need to
* Clone from a repository
``$ git clone https://github.com/nexedi/jio.git``
* Install `NodeJS <http://nodejs.org/>`_ (including ``npm``)
* Install the Grunt command line with ``npm``.
``# npm -g install grunt-cli``
* Install the dependencies.
``$ npm install``
* Compile the JS/CC parser.
``$ make`` (until we find out how to compile it with grunt)
* Run build.
``$ grunt``
Naming Conventions
------------------
All the code follows this :ref:`JavaScript Style Guide <style-guide>`.
How to design your own jIO Storage Library
------------------------------------------
Create a constructor:
.. code-block:: javascript
function MyStorage(storage_description) {
this._value = storage_description.value;
if (typeof this._value !== 'string') {
throw new TypeError("'value' description property " +
"is not a string");
}
}
Create 10 methods: ``post``, ``put``, ``putAttachment``, ``get``, ``getAttachment``,
``remove``, ``removeAttachment``, ``allDocs``, ``check`` and ``repair``.
.. code-block:: javascript
MyStorage.prototype.post = function(command, metadata, option) {
var document_id = metadata._id;
// [...]
};
MyStorage.prototype.get = function(command, param, option) {
var document_id = param._id;
// [...]
};
MyStorage.prototype.putAttachment = function(command, param, option) {
var document_id = param._id;
var attachment_id = param._attachment;
var attachment_data = param._blob;
// [...]
};
// [...]
(To help you design your methods, some tools are provided by jIO.util.)
The first parameter command provides some methods to act on the jIO job:
* ``success``, to tell jIO that the job is successfully terminated
``command.success(status[Text], [{custom key to add to the response}]);``
* ``resolve``, is equal to success
* ``error``, to tell jIO that the job cannot be done
``command.error(status[Text], [reason], [message], [{custom key to add to the response}])``
* ``retry``, to tell jIO that the job cannot be done now, but can be retried later. (same API than error)
* ``reject``, to tell jIO that the job cannot be done, let jIO to decide whether to retry or not. (same API than error)
The second parameter ``metadata`` or ``param`` is the first parameter provided by the jIO user.
The third parameter ``option`` is the option parameter provided by the jIO user.
Methods should return the following objects:
* **post()** --> ``success("created", {"id": new_generated_id})``
* **put()**, **remove()**, **putAttachment()** or **removeAttachment()** --> ``success(204)``
* **get()** --> ``success("ok", {"data": document_metadata})``
* **getAttachment()** -->
.. code-block:: javascript
success("ok", {"data": binary_string, "content_type": content_type})
// or
success("ok", {"data": new Blob([data], {"type": content_type})})
* **allDocs()** --> ``success("ok", {"data": row_object})``
* **check()** -->
.. code-block:: javascript
// if metadata provides "_id" -> check document state
// if metadata doesn't promides "_id" -> check storage state
success("no_content")
// or
error("conflict", "corrupted", "incoherent document or storage")
* **repair()** -->
.. code-block:: javascript
// if metadata provides "_id" -> repair document state
// if metadata doesn't promides "_id" -> repair storage state
success("no_content")
// or
error("conflict", "corrupted", "impossible to repair document or storage")
// DON'T DESIGN STORAGES IF THERE IS NO WAY
// TO REPAIR INCOHERENT STATES
After creating all methods, your storage must be added to jIO. This is done
with the ``jIO.addStorage()`` method, which requires two parameters: the storage
type (string) and a constructor (function). It is called like this:
.. code-block:: javascript
// add custom storage to jIO
jIO.addStorage('mystoragetype', MyStorage);
Please refer to *localstorage.js* implementation for a good example on how to
setup a storage and what methods are required.
Also keep in mind that jIO is a job-based library: whenever you trigger a method,
a job is created, which will later return a response, after being processed.
Job rules
---------
The jIO job manager follows several rules set at the creation of a new jIO
instance. When you try to call a method, jIO will create a job and will make
sure the job is really necessary and will be executed. Thanks to these job
rules, jIO knows what to do with the new job before adding it to the queue. You
can also add your own rules, as we're going to see now.
These are the jIO **default rules**:
.. code-block:: javascript
var jio_instance = jIO.createJIO(storage_description, {
"job_rules": [{
"code_name": "readers update",
"conditions": [
"sameStorageDescription",
"areReaders",
"sameMethod",
"sameParameters",
"sameOptions"
],
"action": "update"
}, {
"code_name": "metadata writers update",
"conditions": [
"sameStorageDescription",
"areWriters",
"useMetadataOnly",
"sameMethod",
"haveDocumentIds",
"sameParameters"
],
"action": "update"
}, {
"code_name": "writers wait",
"conditions": [
"sameStorageDescription",
"areWriters",
"haveDocumentIds",
"sameDocumentId"
],
"action": "wait"
}]
});
The following actions can be used:
* ``ok`` - accept the job
* ``wait`` - wait until the end of the selected job
* ``update`` - bind the selected job to this one
* ``deny`` - reject the job
The following condition functions can be used:
* ``sameStorageDescription`` - check if the storage descriptions are different.
* ``areWriters`` - check if the commands are ``post``, ``put``, ``putAttachment``, ``remove``, ``removeAttachment``, or ``repair``.
* ``areReaders`` - check if the commands are ``get``, ``getAttachment``, ``allDocs`` or ``check``.
* ``useMetadataOnly`` - check if the commands are ``post``, ``put``, ``get``, ``remove`` or ``allDocs``.
* ``sameMethod`` - check if the commands are equal.
* ``sameDocumentId`` - check if the document ids are equal.
* ``sameParameters`` - check if the metadata or param are equal (deep comparison).
* ``sameOptions`` - check if the command options are equal.
* ``haveDocumentIds`` - test if the two commands contain document ids.
Create Job Condition
--------------------
You can create two types of function: job condition, and job comparison.
.. code-block:: javascript
// Job Condition
// Check if the job is a get command
jIO.addJobRuleCondition("isGetMethod", function (job) {
return job.method === 'get';
});
// Job Comparison
// Check if the jobs have the same 'title' property
// only if they are strings
jIO.addJobRuleCondition("sameTitleIfString",
function (job, selected_job) {
if (typeof job.kwargs.title === 'string' &&
typeof selected_job.kwargs.title === 'string') {
return job.kwargs.title === selected_job.kwargs.title;
}
return false;
});
Add job rules
-------------
You just have to define job rules in the jIO options:
.. code-block:: javascript
// Do not accept to post or put a document which title is equal
// to another already running post or put document title
var jio_instance = jIO.createJIO(storage_description, {
"job_rules": [{
"code_name": "avoid similar title",
"conditions": [
"sameStorageDescription",
"areWriters",
"sameTitleIfString"
],
"action": "deny",
"before": "writers update" // optional
// "after": also exists
}]
});
Clear/Replace default job rules
-------------------------------
If a job's ``code_name`` is equal to ``readers update``, then adding this rule will replace it:
.. code-block:: javascript
var jio_instance = jIO.createJIO(storage_description, {
"job_rules": [{
"code_name": "readers update",
"conditions": [
"sameStorageDescription",
"areReaders",
"sameMethod",
"haveDocumentIds"
"sameParameters"
// sameOptions is removed
],
"action": "update"
}]
});
Or you can just clear all rules before adding new ones:
.. code-block:: javascript
var jio_instance = jIO.createJIO(storage_description, {
"clear_job_rules": true,
"job_rules": [{
// ...
}]
});
Getting started
===============
#. :ref:`Download <download-fork>` the core jIO, the storages you need and the
dependencies required for them.
#. Add the scripts to your HTML page in the following order:
.. code-block:: html
<!-- jio core + dependencies -->
<script src="sha256.amd.js"></script>
<script src="rsvp-custom.js"></script>
<script src="jio.js"></script>
<!-- storages + dependencies -->
<script src="complex_queries.js"></script>
<script src="localstorage.js"></script>
<script src="davstorage.js"></script>
<script ...>
With `RequireJS <http://requirejs.org/>`_, the main.js will look like:
.. code-block:: javascript
require.config({
paths: {
// jio core + dependency
sha256: 'sha256.amd', // AMD-compatible version of sha256.js
rsvp: 'rsvp-custom',
jio: 'jio',
// storages + dependencies
complex_queries: 'complex_queries',
localstorage: 'localstorage',
davstorage: 'davstorage'
}
});
#. jIO connects to a number of storages and allows adding handlers (or
functions) to specific storages.
You can use both handlers and available storages to build a storage
tree across which all documents will be maintained and managed by jIO.
.. code-block:: javascript
// create your jio instance
var my_jio = jIO.createJIO(storage_description);
You have to provide a ``storage_description`` object, providing location
and credentials.
Its format depends on the type of storage,
see :ref:`List of Available Storages <list-of-available-storages>`.
#. The jIO API provides ten main methods to manage documents across the storage(s) specified in your jIO storage tree.
For details on the ``document`` and ``attachment`` objects, see :ref:`What is a document? <what-is-a-document>`
======================= ======================================================
Method Example
======================= ======================================================
``.post()`` | ``my_jio.post(document, [options]);``
| Creates a new document
``.put()`` | ``my_jio.put(document, [options]);``
| Creates/Updates a document
``.putAttachment()`` | ``my_jio.putAttachement(attachment, [options]);```
| Updates/Adds an attachment to a document
``.get()`` | ``my_jio.get(document, [options]);``
| Reads a document
``.getAttachment()`` | ``my_jio.getAttachment(attachment, [options]);``
| Reads a document attachment
``.remove()`` | ``my_jio.remove(document, [options]);``
| Deletes a document and its attachments
``.removeAttachment()`` | ``my_jio.removeAttachment(attachment, [options]);``
| Deletes a document's attachment
``.allDocs()`` | ``my_jio.allDocs([options]);``
| Retrieves a list of existing documents
``.check()`` | ``my_jio.check(document, [options]);``
| Checks the document state
``.repair()`` | ``my_jio.repair(document, [options]);``
| Repairs the document
======================= ======================================================
.. _download-fork:
Download & Fork
---------------
Please note that the current (2.0.0-wip) version is not stable yet.
You can use one of the ZIP packages, which include all the dependencies and storages:
`Full download (172k) <_static/jio-2.0.0-wip.zip>`_
`Minified download (87k) <_static/jio-2.0.0-wip-min.zip>`_
or you can create your own set of files, which are are provided in the above packages and the source repository:
Core
^^^^
* sha256.amd.js
* rsvp-custom.js, AMD only version: rsvp-custom.amd.js
* jio.js
* complex_queries.js
Storage dependencies
^^^^^^^^^^^^^^^^^^^^
.. XXX this is a little confusing. Also, the link to sha1.js is broken (404)
* `jquery.js <http://code.jquery.com/jquery.js>`_
* `Stanford Javascript Crypto Library <http://bitwiseshiftleft.github.io/sjcl/>`_, [`sjcl.zip <https://crypto.stanford.edu/sjcl/sjcl.zip>`_]
* `sha1 <http://pajhome.org.uk/crypt/md5/sha1.html>`_, [`sha1.js <http://git.erp5.org/gitweb/jio.git/blob_plain/refs/heads/master:/lib/jsSha1/sha1.js>`_], AMD-compatible version: `sha1.amd.js <http://git.erp5.org/gitweb/jio.git/blob_plain/refs/heads/master:/src/sha1.amd.js>`_
* `sha2, sha256 <http://anmar.eu.org/projects/jssha2/>`_, `jssha2.zip <http://anmar.eu.org/projects/jssha2/files/jssha2-0.3.zip>`_, AMD-compatible versions: `sha2.amd.js <http://git.erp5.org/gitweb/jio.git/blob_plain/refs/heads/master:/src/sha2.amd.js>`_, `sha256.amd.js <http://git.erp5.org/gitweb/jio.git/blob_plain/refs/heads/master:/src/sha256.amd.js>`_
Storage connectors
^^^^^^^^^^^^^^^^^^
* localstorage.js
* davstorage.js
* s3storage.js (depends on sha1, jQuery) (WIP)
* xwikistorage.js (depends on jQuery) (WIP)
* erp5storage.js (depends on jQuery) (WIP)
* restsqlstorage.js (depends on jQuery) (WIP)
* mioga2storage.js (depends on jQuery) (WIP)
Storage handlers
^^^^^^^^^^^^^^^^
* indexstorage.js (WIP)
* gidstorage.js (WIP)
* splitstorage.js (WIP)
* replicatestorage.js (WIP)
Revision based storage handlers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* revisionstorage.js (depends on sha256) (WIP)
* replicaterevisionstorage.js (WIP)
Unit tests
^^^^^^^^^^
We monitor code quality with a `test agent <http://www.j-io.org/quality/unit_test>`_ that runs
the test suite with each release.
Fork jIO
^^^^^^^^
The same source code is kept in three synchronized repositories.
Feel free to use any of them.
* `GitHub <https://github.com/nexedi/jio>`_: ``git clone https://github.com/nexedi/jio.git``
* `Gitorius <https://gitorious.org/nexedi/jio>`_: ``git clone https://git.gitorious.org/nexedi/jio.git``
* `Git Erp5 <http://git.erp5.org/gitweb/jio.git>`_ (read only): ``git clone http://git.erp5.org/repos/jio.git``
.. _gid-storage:
jIO GIDStorage
==============
A storage to enable interoperability between all kind of storages.
A global ID (GID) is a document id which represents a unique document. This ID
is then used to find this unique document on all types of backends.
This storage uses sub storage ``.allDocs()`` and complex queries to find unique documents, and converts their ids to gids.
Where it can be used
--------------------
When you want to duplicate / synchronize / split / edit data in different kind of storages (ERP5 + XWiki + Dav + ...).
Storage Description
-------------------
* ``type`` - ``"gid"``
* ``sub_storage`` - the sub storage description.
* ``constraints`` - the constraints to use to generate a gid by defining metadata types for some kind of document.
Example:
.. code-block:: javascript
{
"type": "gid",
"sub_storage": {<storage description>},
"constraints": {
"default": { // constraints for all kind of documents
// "document metadata": "type of metadata"
"type": "list"
"title": "string"
},
"Text": { // document of type 'Text' additional constraints
"language": "string"
}
}
}
This description tells the *GIDStorage* to use 2 metadata attributes (``type``, ``title``) to define a
document as unique in the default case. If the document is of type ``Text``, then
the handler will use 3 metadata (``type``, ``title``, ``language``).
If these constraints are not respected, then the storage returns an error telling us to
review the document metadata. Here are samples of document respecting the above
constraints:
.. code-block:: javascript
{
"type": "Text",
"title": "Hello World!",
"language": "en"
}
{
"type": ["Text", "Web Page"],
"title": "My Web Page Title",
"language": "en-US",
"format": "text/html"
}
{
"type": "Image",
"title": "My Image Title"
}
Available metadata types are:
* ``"json"`` - The json value of the metadata.
* ``"string"`` - The value as string if it is not a list.
* ``"list"`` - The value as list.
* ``"date"`` - The value if it can be converted to a date (as string).
* ``"DCMIType"`` - A value matching one of the DCMIType Vocabulary (as string).
* ``"contentType"`` - A value which is a content type (as string).
* ``["DCMIType", "list"]`` - The value which contains a DCMIType (as list).
* ``[...]`` - make your own combination.
Document Requirements
---------------------
A metadata value must be a string. This string can be placed in an attribute within
a ``"content"`` key. The object can contains custom keys with string values. A
metadata object can contain several values. Example:
.. code-block:: javascript
{
"key": "value",
// or
"key": ["value1", "value2"],
// or
"key": {
"attribute name": "attribute value",
"content": "value"
},
// or
"key": [
{"scheme": "DCTERMS.URI", "content": "http://foo.com/bar"},
"value2",
"value3",
...
],
...
}
Metadata attributes which names begin with an underscore can contain anything.
.. code-block:: javascript
{
"_key": {"whatever": ["blue", []], "a": null}
}
Storage Requirements
--------------------
* This storage is not compatible with *RevisionStorage* and *ReplicateRevisionStorage*.
* Sub storages have to support options for ``complex queries`` and ``include_docs``.
Dependencies
------------
No dependency.
Suggested storage tree
----------------------
Replication between storages::
Replicate Storage
+-- GID Storage
| `-- Local Storage
+-- GID Storage
| `-- Remote Storage 1
`-- GID Storage
`-- Remote Storage 2
**CAUTION: All gid storage must have the same description!**
Offline application usage::
Replicate Storage
+-- Index Storage with DB in Local Storage
| `-- GID Storage
| `-- ERP5 Storage
`-- GID Storage
`-- Local Storage
**CAUTION: All gid storage must have the same description!**
Welcome to jIO
==============
jIO is a JavaScript library that allows to manage JSON documents on local or
remote storages in asynchronous fashion. jIO is an abstracted API mapped after
CouchDB, that offers connectors to multiple storages, special handlers to
enhance functionality (replication, revisions, indexing) and a query module to
retrieve documents and specific information across storage trees.
How does it work?
-----------------
jIO is composed of two parts - jIO core and storage libraries. The core
makes use of storage libraries (connectors) to interact with the associated remote
storage servers. Some queries can be used on top of the ``.allDocs()`` method to
query documents based on defined criteria.
jIO uses a job management system, so each method call adds a job into a
queue. The queue is copied in the browser's local storage (by default), so it
can be restored in case of browser crash. Jobs are invoked
asynchronously and ongoing jobs are not able to re-trigger to prevent
conflicts.
Copyright and license
---------------------
jIO is open source and is licensed under the `LGPL <http://en.wikipedia.org/wiki/GNU_Lesser_General_Public_License>`_ license.
jIO documentation
-----------------
.. toctree::
:maxdepth: 2
getting_started
manage_documents
revision_storages
available_storages
gid_storage
complex_queries
keys
metadata
developers
style_guide
authors
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="250"
height="250"
id="svg2"
version="1.1"
inkscape:version="0.48.3.1 r9886"
sodipodi:docname="jio-logo.svg">
<defs
id="defs4" />
<sodipodi:namedview
id="base"
pagecolor="transparent"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageopacity="0.0"
inkscape:pageshadow="2"
inkscape:zoom="1.017561"
inkscape:cx="197.65953"
inkscape:cy="220.57125"
inkscape:document-units="px"
inkscape:current-layer="layer2"
showgrid="false"
inkscape:window-width="1366"
inkscape:window-height="748"
inkscape:window-x="-2"
inkscape:window-y="17"
inkscape:window-maximized="1">
<sodipodi:guide
position="0,0"
orientation="0,744.09448"
id="guide2987" />
<sodipodi:guide
position="744.09448,0"
orientation="-1052.3622,0"
id="guide2989" />
<sodipodi:guide
position="744.09448,1052.3622"
orientation="0,-744.09448"
id="guide2991" />
<sodipodi:guide
position="0,1052.3622"
orientation="1052.3622,0"
id="guide2993" />
</sodipodi:namedview>
<metadata
id="metadata7">
<rdf:RDF>
<cc:Work
rdf:about="">
<dc:format>image/svg+xml</dc:format>
<dc:type
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
<dc:title />
</cc:Work>
</rdf:RDF>
</metadata>
<g
inkscape:label="Layer 1"
inkscape:groupmode="layer"
id="layer1"
style="display:inline"
transform="translate(0,-802.36218)">
<path
style="fill:none;stroke:#000000;stroke-width:12.93378448px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
d="m 170.9362,864.54712 0,103.8471"
id="path3008"
inkscape:connector-curvature="0" />
</g>
<g
inkscape:groupmode="layer"
id="layer2"
inkscape:label="Layer 2"
style="display:inline"
transform="translate(0,-802.36218)">
<path
sodipodi:type="arc"
style="color:#000000;fill:none;stroke:#000000;stroke-width:13.67287827;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"
id="path2988-2"
sodipodi:cx="221.71036"
sodipodi:cy="250.89195"
sodipodi:rx="79.000244"
sodipodi:ry="79.000244"
d="m 300.7106,250.89195 a 79.000244,79.000244 0 1 1 -158.00049,0 79.000244,79.000244 0 1 1 158.00049,0 z"
transform="matrix(0.85178613,0,0,0.85178613,-17.864163,748.865)" />
<g
transform="matrix(22.913133,0,0,25.241134,-5160.1455,-16539.144)"
style="font-size:12px;font-style:normal;font-weight:normal;line-height:125%;letter-spacing:0px;word-spacing:0px;fill:#000000;fill-opacity:1;stroke:none;font-family:Sans"
id="flowRoot3780">
<path
d="m 225.958,694.704 c 0.168,0 0.3,0.052 0.396,0.156 0.096,0.104 0.16,0.22 0.192,0.348 0.032,0.128 0.076,0.244 0.132,0.348 0.056,0.104 0.132,0.156 0.228,0.156 0.304,0 0.456,-0.264 0.456,-0.792 l 0,-5.556 c 0,-0.43999 -0.064,-0.71999 -0.192,-0.84 -0.128,-0.12799 -0.436,-0.20799 -0.924,-0.24 l 0,-0.228 3.444,0 0,0.228 c -0.488,0.032 -0.796,0.11201 -0.924,0.24 -0.12,0.12001 -0.18,0.40001 -0.18,0.84 l 0,4.44 c 0,0.752 -0.176,1.336 -0.528,1.752 -0.352,0.408 -0.852,0.612 -1.5,0.612 -0.344,0 -0.628,-0.08 -0.852,-0.24 -0.224,-0.16 -0.336,-0.368 -0.336,-0.624 0,-0.16 0.056,-0.3 0.168,-0.42 0.12,-0.12 0.26,-0.18 0.42,-0.18"
style="font-variant:normal;font-stretch:normal;font-family:FreeSerif;-inkscape-font-specification:FreeSerif"
id="path3832"
inkscape:connector-curvature="0" />
</g>
</g>
</svg>
Search Keys
===========
Features like case insensitive, accent-removing, full-text searches and more can be implemented
by customizing jIO's query behavior.
Let's start with a simple search:
.. code-block:: javascript
var query = {
type: 'simple',
key: 'someproperty',
value: comparison_value,
operator: '='
}
Each of the ``.someproperty`` attribute in objects' metadata is compared with
``comparison_value`` through a function defined by the '=' operator. Normally,
it would be a string match that uses the wildcard_character, if present.
You can provide your own function to be used as '=' operator:
.. code-block:: javascript
var strictEqual = function (object_value, comparison_value,
wildcard_character) {
return comparison_value === object_value;
};
var query = {
type: 'simple',
key: {
read_from: 'someproperty',
equal_match: strictEqual
},
value: comparison_value
}
Inside ``equal_match``, you can decide to interpret the ``wildcard_character``
or just ignore it, as in this case.
If you need to convert or preprocess the values before comparison, you can provide
a conversion function:
.. code-block:: javascript
var numberType = function (obj) {
return parseFloat('3.14');
};
var query = {
type: 'simple',
key: {
read_from: 'someproperty',
cast_to: numberType
},
value: comparison_value
}
In this case, the operator is still the default '=' that works with strings.
You can combine ``cast_to`` and ``equal_match``:
.. code-block:: javascript
var query = {
type: 'simple',
key: {
read_from: 'someproperty',
cast_to: numberType,
equal_match: strictEqual
},
value: comparison_value
}
Now the query returns all objects for which the following is true:
.. code-block:: javascript
strictEqual(numberType(metadata.someproperty),
numberType(comparison_value))
For a more useful example, the following function removes the accents
from any string:
.. code-block:: javascript
var accentFold = function (s) {
var map = [
[new RegExp('[àáâãäå]', 'gi'), 'a'],
[new RegExp('æ', 'gi'), 'ae'],
[new RegExp('ç', 'gi'), 'c'],
[new RegExp('[èéêë]', 'gi'), 'e'],
[new RegExp('[ìíîï]', 'gi'), 'i'],
[new RegExp('ñ', 'gi'), 'n'],
[new RegExp('[òóôõö]', 'gi'), 'o'],
[new RegExp('œ', 'gi'), 'oe'],
[new RegExp('[ùúûü]', 'gi'), 'u'],
[new RegExp('[ýÿ]', 'gi'), 'y']
];
map.forEach(function (o) {
var rep = function (match) {
if (match.toUpperCase() === match) {
return o[1].toUpperCase();
}
return o[1];
};
s = s.replace(o[0], rep);
});
return s;
};
...
cast_to: accentFold
...
A more robust solution to manage diacritics is recommended for production
environments, with unicode normalization, like (untested):
https://github.com/walling/unorm/
Overriding operators and sorting
--------------------------------
The advantage of providing an ``equal_match`` function is that it can work with basic types;
you can keep the values as strings or, if you use a ``cast_to`` function, it can return strings,
numbers, arrays... and that's fine if all you need is the '=' operator.
It's also possible to customize the behavior of the other operators: <, >, !=...
To do that, the object returned by ``cast_to`` must contain a ``.cmp``
property, that behaves like the ``compareFunction`` described in
`Array.prototype.sort() <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort>`_:
.. code-block:: javascript
function myType (...) {
...
return {
...
'cmp': function (b, wildcard_character) {
if (a < b) {
return -1;
}
if (a > b) {
return +1;
}
return 0;
}
};
}
...
cast_to: myType
...
``wildcard_character`` is only passed by ``=`` and ``!=`` operators.
If the < or > comparison makes no sense for the objects, the function should return ``undefined``.
The ``.cmp()`` property is also used, if present, by the sorting feature of complex queries.
Partial Date/Time match
-----------------------
As a real life example, consider a list of documents that have a *start_task* property.
The value of ``start_task`` can be an `ISO 8601 <http://en.wikipedia.org/wiki/ISO_8601>`_ string
with date and time information including fractions of a second. Which is, honestly, a bit too
much for most queries.
By using a ``cast_to`` function with custom operators, it is possible to perform queries like
"start_task > 2010-06", or "start_task != 2011". Partial time can be used as well, so
we can ask for projects started after noon of a given day: ``start_task = "2011-04-05" AND start_task > "2011-04-05 12"``
The JIODate type has been implemented on top of the `Moment.js <http://momentjs.com/>`_ library, which
has a rich API with support for multiple languages and timezones. No special support for timezones
is present (yet) in JIODate.
To use JIODate, include the ``jiodate.js`` and ``moment.js`` files in your
application, then set ``cast_to = jiodate.JIODate``.
Key Schemas
-----------
Instead of providing the key object for each attribute you want to filter,
you can group all of them in a schema object for reuse:
.. code-block:: javascript
var key_schema = {
key_set: {
date_day: {
read_from: 'date',
cast_to: 'dateType',
equal_match: 'sameDay'
},
date_month: {
read_from: 'date',
cast_to: 'dateType',
equal_match: 'sameMonth'
}
},
cast_lookup: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
return obj;
}
return new Date(obj);
}
},
match_lookup: {
sameDay: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth()) &&
(a.getDate() === b.getDate())
);
},
sameMonth: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth())
);
}
}
}
With this schema, we have created two 'virtual' metadata attributes,
``date_day`` and ``date_month``. When queried, they match values that
happen to be in the same day, ignoring the time, or the same month, ignoring
both time and day.
A key_schema object can have three properties:
* ``key_set`` - required.
* ``cast_lookup`` - optional, a mapping of name: function that will
be used if cast_to is a string. If cast_lookup is not provided,
then cast_to must be a function.
* ``match_lookup`` - optional, a mapping of name: function that will
be used if ``equal_match`` is a string. If match_lookup is not provided,
then ``equal_match`` must be a function.
Using a schema
--------------
A schema can be used:
* In a query constructor. The same schema will be applied to all the sub-queries:
.. code-block:: javascript
complex_queries.QueryFactory.create({...}, key_schema).exec(...);
* In the ``jIO.createJIO()`` method. The same schema will be used
by all the queries created with the ``.allDocs()`` method:
.. code-block:: javascript
var jio = jIO.createJIO({
type: 'local',
username: '...',
application_name: '...',
key_schema: key_schema
});
How to manage documents?
========================
jIO is mapped after the CouchDB APIs and extends them to provide unified, scalable
and high performance access via JavaScript to a wide variety of storage backends.
If you are not familiar with `Apache CouchDB <http://couchdb.apache.org/>`_:
it is a scalable, fault-tolerant, and schema-free document-oriented database.
It is used in large and small organizations for a variety of applications where
traditional SQL databases are not the best solution for the problem at hand.
CouchDB provides a RESTful HTTP/JSON API accessible by many programming
libraries and tools (like `curl <http://curl.haxx.se/>`_ or `Pouchdb <http://pouchdb.com/>`_)
and has its own conflict management system.
.. _what-is-a-document:
What is a document?
-------------------
A document is an association of metadata and attachment(s). The metadata is the
set of properties of the document and the attachments are binary (or text) objects
that represent the content of the document.
In jIO, the metadata is a dictionary with keys and values (a JSON object), and
attachments are simple strings.
.. code-block:: javascript
{
// document metadata
_id: 'Identifier',
title: 'A Title!',
creator: 'Mr.Author'
}
You can also retrieve document attachment metadata in this object.
.. code-block:: javascript
{
// document metadata
_id : 'Identifier',
title : 'A Title!',
creator: 'Mr.Author',
_attachments: {
// attachment metadata
'body.html': {
length: 12893,
digest: 'sha256-XXXX...',
content_type: 'text/html'
}
}
}
:ref:`Here <metadata-head>` is a draft about metadata to use with jIO.
Basic Methods
-------------
Below you can see examples of the main jIO methods. All examples are using
revisions (as in revision storage or replicated revision storage), so you can
see how method calls should be made with either of these storages.
.. code-block:: javascript
// Create a new jIO instance
var jio_instance = jIO.createJIO(storage_description);
// create and store new document
jio_instance.post({title: 'some title'}).
then(function (response) {
// console.log(response);
});
// create or update an existing document
jio_instance.put({_id: 'my_document', title: 'New Title'}).
then(function (response) {
// console.log(response);
});
// add an attachment to a document
jio_instance.putAttachment({_id: 'my_document',
_attachment: 'its_attachment',
_data: 'abc',
_mimetype: 'text/plain'}).
then(function (response) {
// console.log(response);
});
// read a document
jio_instance.get({_id: 'my_document'}).
then(function (response) {
// console.log(response);
});
// read an attachment
jio_instance.getAttachment({_id: 'my_document',
_attachment: 'its_attachment'}).
then(function (response) {
// console.log(response);
});
// delete a document and its attachment(s)
jio_instance.remove({_id: 'my_document'}).
then(function (response) {
// console.log(response);
});
// delete an attachment
jio_instance.removeAttachment({_id: 'my_document',
_attachment: 'its_attachment'}).
then(function (response) {
// console.log(response);
});
// get all documents
jio_instance.allDocs().then(function (response) {
// console.log(response);
});
Promises
--------
Each jIO method returns a Promise object, which allows us to get responses into
callback parameters and to chain callbacks with other returned values.
jIO uses a custom version of `RSVP.js <https://github.com/tildeio/rsvp.js>`_, adding canceler and progression features.
You can read more about promises:
* `RSVP.js <https://github.com/tildeio/rsvp.js#rsvpjs-->`_ on GitHub
* `Promises/A+ <http://promisesaplus.com/>`_
* `CommonJS Promises <http://wiki.commonjs.org/wiki/Promises>`_
Method Options and Callback Responses
-------------------------------------
To retrieve jIO responses, you have to provide callbacks like this:
.. code-block:: javascript
jio_instance.post(metadata, [options]).
then([responseCallback], [errorCallback], [progressionCallback]);
* On command success, ``responseCallback`` is called with the jIO response as first parameter.
* On command error, ``errorCallback`` is called with the jIO error as first parameter.
* On command notification, ``progressionCallback`` is called with the storage notification.
Here is a list of responses returned by jIO according to methods and options:
================== ============================================== ===============================================
Option Available for Response (Callback first parameter)
================== ============================================== ===============================================
No options ``.post()``, ``.put()``, ``.remove()`` .. code-block:: javascript
{
result: 'success',
method: 'post',
// or put or remove
id: 'my_doc_id',
status: 204,
statusText: 'No Content'
}
No options ``.putAttachment()``, ``.removeAttachment()`` .. code-block:: javascript
{
result: 'success',
method: 'putAttachment',
// or removeAttachment
id: 'my_doc_id',
attachment: 'my_attachment_id',
status: 204,
statusText: 'No Content'
}
No options ``.get()`` .. code-block:: javascript
{
result: 'success',
method: 'get',
id: 'my_doc_id',
status: 200,
statusText: 'Ok',
data: {
// Here, the document metadata
}
}
No options ``.getAttachment()`` .. code-block:: javascript
{
result: 'success',
method: 'getAttachment',
id: 'my_doc_id',
attachment: 'my_attachment_id',
status: 200,
statusText: 'Ok',
data: Blob // Here, the attachment content
}
No option ``.allDocs()`` .. code-block:: javascript
{
result: 'success',
method: 'allDocs',
id: 'my_doc_id',
status: 200,
statusText: 'Ok',
data: {
total_rows: 1,
rows: [{
id: 'mydoc',
key: 'mydoc', // optional
value: {},
}]
}
}
include_docs: true ``.allDocs()`` .. code-block:: javascript
{
result: 'success',
method: 'allDocs',
id: 'my_doc_id',
status: 200,
statusText: 'Ok',
data: {
total_rows: 1,
rows: [{
id: 'mydoc',
key: 'mydoc', // optional
value: {},
doc: {
// Here, 'mydoc' metadata
}
}]
}
}
================== ============================================== ===============================================
In case of error, the ``errorCallback`` first parameter will look like:
.. code-block:: javascript
{
result: 'error',
method: 'get',
status: 404,
statusText: 'Not Found',
error: 'not_found',
reason: 'document missing',
message: 'Unable to get the requested document'
}
How to store binary data
------------------------
The following example creates a new jIO in localStorage and then posts a document with two attachments.
.. code-block:: javascript
// create a new jIO
var jio_instance = jIO.createJIO({
type: 'local',
username: 'usr',
application_name: 'app'
});
// post the document 'metadata'
jio_instance.post({
title : 'My Video',
type : 'MovingImage',
format : 'video/ogg',
description : 'Images Compilation'
}, function (err, response) {
var id;
if (err) {
return alert('Error posting the document meta');
}
id = response.id;
// post a thumbnail attachment
jio_instance.putAttachment({
_id: id,
_attachment: 'thumbnail',
_data: my_image,
_mimetype: 'image/jpeg'
}, function (err, response) {
if (err) {
return alert('Error attaching thumbnail');
}
// post video attachment
jio_instance.putAttachment({
_id: id,
_attachment: 'video',
_data: my_video,
_mimetype: 'video/ogg'
}, function (err, response) {
if (err) {
return alert('Error attaching the video');
}
alert('Video Stored');
});
});
});
localStorage now contains:
.. code-block:: javascript
{
"jio/local/usr/app/12345678-1234-1234-1234-123456789012": {
"_id": "12345678-1234-1234-1234-123456789012",
"title": "My Video",
"type": "MovingImage",
"format": "video/ogg",
"description": "Images Compilation",
"_attachments":{
"thumbnail":{
"digest": "md5-3ue...",
"content_type": "image/jpeg",
"length": 17863
},
"video":{
"digest": "md5-0oe...",
"content_type": "video/ogg",
"length": 2840824
}
}
},
"jio/local/usr/app/myVideo/thumbnail": "/9j/4AAQSkZ...",
"jio/local/usr/app/myVideo/video": "..."
}
.. _metadata-head:
Metadata
========
What is metadata?
-----------------
The word "metadata" means "data about data". Metadata articulates a context for
objects of interest -- "resources" such as MP3 files, library books, or
satellite images -- in the form of "resource descriptions". As a tradition,
resource description dates back to the earliest archives and library catalogs.
During the Web revolution of the mid-1990s, `Dublin Core <http://dublincore.org/metadata-basics/>`_
has emerged as one of the prominent metadata standards.
Why use metadata?
-----------------
Uploading a document to several servers can be very tricky, because the
document has to be saved in a place where it can be easily found with basic
searches in all storages (For instance: ERP5, XWiki and Mioga2 have their own
way to save documents and to get them). So we must use metadata for
*interoperability reasons*. Interoperability is the ability of diverse systems
and organizations to work together.
How to format metadata with jIO
-------------------------------
See below XML and its JSON equivalent:
+--------------------------------------------+---------------------------------------+
| XML | JSON |
+============================================+=======================================+
| .. code-block:: xml | .. code-block:: javascript |
| | |
| <dc:title>My Title</dc:title> | {"title":"My Title"} |
+--------------------------------------------+---------------------------------------+
| .. code-block:: xml | .. code-block:: javascript |
| | |
| <dc:contributor>Me</dc:contributor> | {"contributor":["Me", "And You"]} |
| <dc:contributor>And You</dc:contributor> | |
+--------------------------------------------+---------------------------------------+
| .. code-block:: xml | .. code-block:: javascript |
| | |
| <dc:identifier scheme="DCTERMS.URI"> | {"identifier": [ |
| http://my/resource | { |
| </dc:identifier> | "scheme": "DCTERMS.URI", |
| <dc:identifier> | "content": "http://my/resource" |
| Xaoe41PAPNIWz | }, |
| </dc:identifier> | "Xaoe41PAPNIWz"]} |
+--------------------------------------------+---------------------------------------+
List of metadata to use
-----------------------
Identification
^^^^^^^^^^^^^^
* **_id**
A specific jIO metadata which helps the storage to find a document
(can be a real path name, a dc:identifier, a uuid, ...). **String Only**
* **identifer**
| ``{"identifier": "http://domain/jio_home_page"}``
| ``{"identifier": "urn:ISBN:978-1-2345-6789-X"}``
| ``{"identifier": [{"scheme": "DCTERMS.URI", "content": "http://domain/jio_home_page"}]}``
An unambiguous reference to the resource within a given context. Recommended
best practice is to identify the resource with a string or number
conforming to a formal identification system. Examples of formal identification
systems include the `Uniform Resource Identifier <http://en.wikipedia.org/wiki/URI>`_ (URI)
(including the `Uniform Resource Locator <http://en.wikipedia.org/wiki/URL>`_ (URL),
the `Digital Object Identifier <http://en.wikipedia.org/wiki/Digital_object_identifier>`_ (DOI)
and the `International Standard Book Number <http://en.wikipedia.org/wiki/Isbn>`_ (ISBN).
* **format**
| ``{"format": ["text/html", "52 kB"]}``
| ``{"format": ["image/jpeg", "100 x 100 pixels", "13.2 KiB"]}``
The physical or digital manifestation of the resource. Typically, Format may
include the media-type or dimensions of the resource. Examples of dimensions
include size and duration. Format may be used to determine the software,
hardware or other equipment needed to display or operate the resource.
* **date**
| ``{"date": "2011-12-13T14:15:16Z"}``
| ``{"date": {"scheme": "DCTERMS.W3CDTF", "content": "2011-12-13"}}``
A date associated with an event in the life cycle of the resource. Typically,
Date will be associated with the creation or availability of the resource.
Recommended best practice for encoding the date value is defined in a profile
of ISO 8601 `Date and Time Formats, W3C Note <http://www.w3.org/TR/NOTE-datetime>`_
and follows the YYYY-MM-DD format.
* **type**
| ``{"type": "Text"}``
| ``{"type": "Image"}``
| ``{"type": "Dataset"}``
The nature or genre of the content of the resource. Type includes terms describing
general categories, functions, genres, or aggregation levels for content.
Recommended best practice is to select a value from a controlled vocabulary.
**The type is not a MIME Type!**
Intellectual property
^^^^^^^^^^^^^^^^^^^^^
* **creator**
| ``{"creator": "Tristan Cavelier"}``
| ``{"creator": ["Tristan Cavelier", "Sven Franck"]}``
An entity primarily responsible for creating the content of the resource.
Examples of a Creator include a person, an organization, or a service.
Typically the name of the Creator should be used to indicate the entity.
* **publisher**
| ``{"publisher": "Nexedi"}``
The entity responsible for making the resource available. Examples of a
Publisher include a person, an organization, or a service. Typically, the name
of a Publisher should be used to indicate the entity.
* **contributor**
| ``{"contributor": ["Full Name", "Full Name", ...]}``
An entity responsible for making contributions to the content of the
resource. Examples of a Contributor include a person, an organization or a
service. Typically, the name of a Contributor should be used to indicate the
entity.
* **rights**
| ``{"rights": "Access limited to members"}``
| ``{"rights": "https://www.j-io.org/documentation/jio-documentation/#copyright-and-license"}``
Information about rights held in and over the resource. Typically a Rights
element will contain a rights management statement for the resource, or
reference a service providing such information. Rights information often
encompasses Intellectual Property Rights (IPR), Copyright, and various Property
Rights. If the rights element is absent, no assumptions can be made about the
status of these and other rights with respect to the resource.
Content
^^^^^^^
* **title**
| ``{"title": "jIO Home Page"}``
The name given to the resource. Typically, a Title will be a name by which the resource is formally known.
* **subject**
| ``{"subject": "jIO"}``
| ``{"subject": ["jIO", "basics"]}``
The topic of the content of the resource. Typically, a Subject will be
expressed as keywords or key phrases or classification codes that describe the
topic of the resource. Recommended best practice is to select a value from a
controlled vocabulary or formal classification scheme.
* **description**
| ``{"description": "Simple guide to show the basics of jIO"}``
| ``{"description": {"lang": "fr", "content": "Ma description"}}``
An account of the content of the resource. Description may include but is not
limited to: an abstract, table of contents, reference to a graphical
representation of content or a free-text account of the content.
* **language**
| ``{"language": "en"}``
The language of the intellectual content of the resource. Recommended best
practice for the values of the Language element is defined by `RFC 3066 <http://www.ietf.org/rfc/rfc3066.txt>`_
which, in conjunction with `ISO 639 <http://www.oasis-open.org/cover/iso639a.html>`_, defines two- and
three-letter primary language tags with optional subtags. Examples include "en"
or "eng" for English, "akk" for Akkadian, and "en-GB" for English used in the
United Kingdom.
* **source**
| ``{"source": ["Image taken from a drawing by Mr. Artist", "<phone number>"]}``
A Reference to a resource from which the present resource is derived. The
present resource may be derived from the Source resource in whole or part.
Recommended best practice is to reference the resource by means of a string or
number conforming to a formal identification system.
* **relation**
| ``{"relation": "Resilience project"}``
A reference to a related resource. Recommended best practice is to reference
the resource by means of a string or number conforming to a formal
identification system.
* **coverage**
| ``{"coverage": "France"}``
The extent or scope of the content of the resource. Coverage will typically
include spatial location (a place name or geographic co-ordinates), temporal
period (a period label, date, or date range) or jurisdiction (such as a named
administrative entity). Recommended best practice is to select a value from a
controlled vocabulary (for example, the `Getty Thesaurus of Geographic Names
<http://www.getty.edu/research/tools/vocabulary/tgn/>`_. Where appropriate, named
places or time periods should be used in preference to numeric identifiers such
as sets of co-ordinates or date ranges.
* **category**
| ``{"category": ["parent/26323", "resilience/javascript", "javascript/library/io"]}``
The category the resource is associated with. The categories may look like
navigational facets, they correspond to the properties of the resource which
can be generated with metadata or some other information (see `faceted search <https://en.wikipedia.org/wiki/Faceted_search>`_).
* **product**
| ``{"product": "..."}``
For e-commerce use.
* **custom**
| ``{custom1: value1, custom2: value2, ...}``
Examples
--------
Posting a webpage for jIO
^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: javascript
jio.put({
"_id" : "...",
"identifier" : "http://domain/jio_home_page",
"format" : ["text/html", "52 kB"],
"date" : new Date(),
"type" : "Text",
"creator" : ["Nexedi", "Tristan Cavelier", "Sven Franck"],
"title" : "jIO Home Page",
"subject" : ["jIO", "basics"],
"description": "Simple guide to show the basics of jIO",
"category" : ["resilience/jio", "webpage"],
"language" : "en"
}, callbacks); // send content as attachment
Posting jIO library
^^^^^^^^^^^^^^^^^^^
.. code-block:: javascript
jio.put({
"_id" : "...",
"identifier" : "jio.js",
"date" : "2013-02-15",
"format" : "application/javascript",
"type" : "Software",
"creator" : ["Tristan Cavelier", "Sven Franck"],
"publisher" : "Nexedi",
"rights" :
"https://www.j-io.org/documentation/" +
"jio-documentation/#copyright-and-license",
"title" : "Javascript Input/Output",
"subject" : "jIO",
"category" : [
"resilience/javascript",
"javascript/library/io"
]
"description": "jIO is a client-side JavaScript library to " +
"manage documents across multiple storages."
}, callbacks); // send content as attachment
Posting a webpage for interoperability levels
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: javascript
jio.put({
"_id" : "...",
"identifier" : "http://dublincore.org/documents/" +
"interoperability-levels/",
"date" : "2009-05-01",
"format" : "text/html",
"type" : "Text",
"creator" : [
"Mikael Nilsson",
"Thomas Baker",
"Pete Johnston"
],
"publisher" : "Dublin Core Metadata Initiative",
"title" : "Interoperability Levels for Dublin Core Metadata",
"description": "This document discusses the design choices " +
"involved in designing applications for " +
"different types of interoperability. [...]",
"language" : "en"
}, callbacks); // send content as attachment
Posting an image
^^^^^^^^^^^^^^^^
.. code-block:: javascript
jio.put({
"_id" : "...",
"identifier" : "new_york_city_at_night",
"format" : ["image/jpeg", "7.2 MB", "8192 x 4096 pixels"],
"date" : "1999",
"type" : "Image",
"creator" : "Mr. Someone",
"title" : "New York City at Night",
"subject" : ["New York"],
"description": "A photo of New York City taken just after midnight",
"coverage" : ["New York", "1996-1997"]
}, callbacks); // send content as attachment
Posting a book
^^^^^^^^^^^^^^
.. code-block:: javascript
jio.put({
"_id" : "...",
"identifier" : {
"scheme": "DCTERMS.URI",
"content": "urn:ISBN:0385424728"
},
"format" : "application/pdf",
"date" : {
"scheme": "DCTERMS.W3CDTF",
"content": getW3CDate()
}, // see tools below
"creator" : "Original Author(s)",
"publisher" : "Me",
"title" : {"lang": "en", "content": "..."},
"description": {"lang": "en", "Summary: ..."},
"language" : {
"scheme": "DCTERMS.RFC4646",
"content": "en-GB"
}
}, callbakcs); // send content as attachment
Posting a video
^^^^^^^^^^^^^^^
.. code-block:: javascript
jio.put({
"_id" : "...",
"identifier" : "my_video",
"format" : ["video/ogg", "130 MB", "1080p", "20 seconds"],
"date" : getW3CDate(), // see tools below
"type" : "Video",
"creator" : "Me",
"title" : "My life",
"description": "A video about my life"
}, callbacks); // send content as attachment
Posting a job announcement
^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: javascript
jio.post({
"format" : "text/html",
"date" : "2013-02-14T14:44Z",
"type" : "Text",
"creator" : "James Douglas",
"publisher" : "Morgan Healey Ltd",
"title" : "E-Commerce Product Manager",
"subject" : "Job Announcement",
"description": "...",
"language" : "en-GB",
"source" : "James@morganhealey.com",
"relation" : ["Totaljobs"],
"coverage" : "London, South East",
"job_type" : "Permanent",
"salary" : "£45,000 per annum"
}, callbacks); // send content as attachment
// result: http://www.totaljobs.com/JobSeeking/E-Commerce-Product-Manager_job55787655
Getting a list of document created by someone
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
With complex query:
.. code-block:: javascript
jio.allDocs({"query": "creator: \"someone\""}, callbacks);
Getting all documents about jIO in the resilience project
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
With complex query:
.. code-block:: javascript
jio.allDocs({
"query": 'subject: "jIO" AND category: "resilience"'
},
callbacks);
Tools
-----
W3C Date function
^^^^^^^^^^^^^^^^^
.. code-block:: javascript
/**
* Tool to get the date in W3C date format
* - "2011-12-13T14:15:16+01:00" with use_utc = false (by default)
* - "2011-12-13T13:15:16Z" with use_utc = true
*
* @param {Boolean} use_utc Use UTC format
* @return {String} The date in W3C date format
*/
function getW3CDate(use_utc) {
var d = new Date(), offset;
if (use_utc === true) {
return d.toISOString();
}
offset = - d.getTimezoneOffset();
return (
d.getFullYear() + "-" +
(d.getMonth() + 1) + "-" +
d.getDate() + "T" +
d.getHours() + ":" +
d.getMinutes() + ":" +
d.getSeconds() + "." +
d.getMilliseconds() +
(offset < 0 ? "-" : "+") +
(offset / 60) + ":" +
(offset % 60)
).replace(/[0-9]+/g, function (found) {
if (found.length < 2) {
return '0' + found;
}
return found;
});
}
Sources
-------
* `Interoperability definition <https://en.wikipedia.org/wiki/Interoperability>`_
* `Faceted search <https://en.wikipedia.org/wiki/Faceted_search>`_
* `DublinCore <http://dublincore.org/>`_
* `Interoperability levels <http://dublincore.org/documents/interoperability-levels/>`_
* `Metadata elements <http://dublincore.org/documents/usageguide/elements.shtml>`_
* http://www.chu-rouen.fr/documed/eahilsantander.html
* http://openweb.eu.org/articles/dublin_core (French)
* `CouchDB <https://couchdb.apache.org/>`_
* `Resource Description Framework (RDF) <http://www.w3.org/RDF/>`_
* `Five Ws <https://en.wikipedia.org/wiki/Five_Ws>`_
* `Metadata <https://en.wikipedia.org/wiki/Metadata>`_
* MIME Types
* https://en.wikipedia.org/wiki/Internet_media_type
* https://www.iana.org/assignments/media-types
Revision Storages: Conflicts and Resolution
===========================================
Why Conflicts can Occur
-----------------------
Using jIO you can store documents in multiple locations. With an
increasing number of users working on a document and some storages not being
available or responding too slow, conflicts are more likely to occur. jIO
defines a conflict as multiple versions of a document existing in a storage
tree and a user trying to save on a version that does not match the latest
version of the document.
To keep track of document versions a revision storage must be used. When doing
so, jIO creates a document tree file for every document. This file contains all
existing versions and their status and is modified whenever a version is
added/updated/removed or when storages are being synchronized.
How to solve conflicts
----------------------
Using the document tree, jIO tries to make every version of a document
available on every storage. When multiple versions of a document exist, jIO
will select the **latest**, **left-most** version on the document tree, along with the
conflicting versions (when option **conflicts: true** is set in order for
developers to setup a routine to solve conflicts.
Technically, a conflict is solved by deleting alternative versions of a document
("cutting leaves off from the document tree"). When a user decides to keep a
version of a document and manually deletes all conflicting versions, the
storage tree is updated accordingly and the document is available in a single
version on all storages.
Simple Conflict Example
-----------------------
.. TODO this is a little confusing
You are keeping a namecard file on your PC updating from your smartphone. Your
smartphone ran out of battery and is offline when you update your namecard on
your PC with your new email adress. Someone else changes this email from your PC
and once your smartphone is recharged, you go back online and the previous
update is executed.
#. Set up the storage tree:
.. code-block:: javascript
var jio_instance = jIO.createJIO({
// replicate revision storage
type: 'replicaterevision',
storagelist:[{
type: 'revision',
sub_storage: {
type: 'dav',
...
}
}, {
type: 'revision',
sub_storage: {
type: 'local',
...
}
}]
});
#. Create the namecard on your smartphone:
.. code-block:: javascript
jio_instance.post({
_id: 'myNameCard',
email: 'me@web.com'
}).then(function (response) {
// response.id -> 'myNameCard'
// response.rev -> '1-5782E71F1E4BF698FA3793D9D5A96393'
});
This will create the document on your WebDAV and local storage
#. Someone else updates your shared namecard on WebDAV:
.. code-block:: javascript
jio_instance.put({
email: 'my_new_me@web.com',
_id: 'myNameCard'
_rev: '1-5782E71F1E4BF698FA3793D9D5A96393'
}).then(function (response) {
// response.id -> 'myNameCard'
// response.rev -> '2-068E73F5B44FEC987B51354DFC772891'
});
Your smartphone is offline, so now you will have one version (1-578...) on
your smartphone and another version on WebDAV (2-068...) on your PC.
#. You modify the namecard while being offline:
.. code-block:: javascript
jio_instance.get({_id: 'myNameCard'}).then(function (response) {
// response.id -> 'myNameCard'
// response.rev -> '1-5782E71F1E4BF698FA3793D9D5A96393'
// response.data.email -> 'me@web.com'
return jio_instance.put({
_id: 'myNameCard',
email: 'me_again@web.com'
});
}).then(function (response) {
// response.id -> 'myNameCard'
// response.rev -> '2-3753476B70A49EA4D8C9039E7B04254C'
});
#. Later, your smartphone is online and you retrieve the other version of the namecard:
.. code-block:: javascript
jio_instance.get({_id: 'myNameCard'}).then(function (response) {
// response.id -> 'myNameCard'
// response.rev -> '2-3753476B70A49EA4D8C9039E7B04254C'
// response.data.email -> 'me_again@web.com'
});
When multiple versions of a document are available, jIO returns the latest,
left-most version on the document tree (2-375... and labels all other
versions as conflicting 2-068...).
#. Retrieve conflicts by setting option:
.. code-block:: javascript
jio_instance.get({_id: 'myNameCard'}, {
conflicts: true
}).then(function (response) {
// response.id -> 'myNameCard'
// response.rev -> '2-3753476B70A49EA4D8C9039E7B04254C',
// response.conflicts -> ['2-068E73F5B44FEC987B51354DFC772891']
});
The conflicting version (*2-068E...*) is displayed, because **{conflicts: true}** was
specified in the GET call. Deleting either version will solve the conflict.
#. Delete the conflicting version:
.. code-block:: javascript
jio_instance.remove({
_id: 'myNameCard',
_rev: '2-068E73F5B44FEC987B51354DFC772891'
}).then(function (response) {
// response.id -> 'myNameCard'
// response.rev -> '3-28910A4937537B5168E772896B70EC98'
});
When deleting the conflicting version of your namecard, jIO removed it
from all storages and set the document tree leaf of that version to
*deleted*. All storages now contain just a single version of the namecard
(2-3753...). Note that, on the document tree, removing a revison will
create a new revision with status set to *deleted*.
.. _style-guide:
JavaScript Style Guide
======================
This document defines JavaScript style conventions, which are split into essential, coding and naming conventions.
Essential Conventions
---------------------
Essential conventions include generic patterns that you should adhere to in order to write *readable*, *consistent* and *maintainable* code.
Minimizing Globals
^^^^^^^^^^^^^^^^^^
Variable declarations should always be done using *var* to not declare them as
global variables. This avoids conflicts from using a variable name across
different functions as well as conflicts with global variables declared by third
party plugins.
.. XXX always pub good+bad or bad+good examples in the same order
Good Example
.. code-block:: javascript
function sum(x, y) {
var result = x + y;
return result;
}
Bad Example
.. code-block:: javascript
function sum(x, y) {
// missing var declaration, implied global
result = x + y;
return result;
}
Using JSLint
^^^^^^^^^^^^
`JSLint <http://www.jslint.com/>`_ is a quality tool that inspects code and warns
about potential problems. It can be used online and can also be integrated
into several development environments, so errors can be highlighted while
writing code.
Before validating your code in JSLint, you should use a code
beautifier to fix basic syntax errors (like indentation) automatically. There
are a number of beautifiers available online. The following ones seem to work best:
* `JSbeautifier.org <http://jsbeautifier.org/>`_
* `JS-Beautify <http://alexis.m2osw.com/js-beautify/>`_
In this project, JavaScript sources have to begin with the header:
.. code-block:: javascript
/*jslint indent: 2, maxlen: 80, nomen: true */
which means it uses two spaces indentation, 80
maximum characters per line and allows variable names starting with '_'.
Other JSLint options can be added in sub functions if necessary.
Some allowed options are:
* ``ass: true`` if assignment should be allowed outside of statement position.
* ``bitwise: true`` if bitwise operators should be allowed.
* ``continue: true`` if the continue statement should be allowed.
* ``newcap: true`` if Initial Caps with constructor function is optional.
* ``regexp: true`` if ``.`` and ``[^...]`` should be allowed in RegExp literals. They match more material than might be expected, allowing attackers to confuse applications. These forms should not be used when validating in secure applications.
* ``unparam: true`` if warnings should be silenced for unused parameters.
Coding Conventions
------------------
Coding conventions include generic patterns that ensure the written code is consistently formatted.
Using two-space indentation
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Tabs and 2-space indentation are being used equally. Since a lot of errors on
JSLint often result from mixed use of space and tab, using 2 spaces throughout
prevents these errors up front.
Good Example
.. code-block:: javascript
function outer(a, b) {
var c = 1,
d = 2,
inner;
if (a > b) {
inner = function () {
return {
"r": c - d
};
};
} else {
inner = function () {
return {
"r": c + d
};
};
}
return inner;
}
Bad Example
.. code-block:: javascript
function outer(a, b) {
var c = 1,
d = 2,
inner;
if (a > b) {
inner = function () {
return {
r: c - d
}}}};
Using shorthand for conditional statements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
An alternative for using braces is the shorthand notation for conditional
statements. When using multiple conditions, the conditional statement can be
split on multiple lines.
Good Example
.. code-block:: javascript
// single line
var results = test === true ? alert(1) : alert(2);
// multiple lines
var results = (test === true && number === undefined ?
alert(1) : alert(2));
var results = (test === true ?
alert(1) : number === undefined ?
alert(2) : alert(3));
Bad Example
.. code-block:: javascript
// multiple conditions
var results = (test === true && number === undefined) ?
alert(1) :
alert(2);
Opening Brace Location
^^^^^^^^^^^^^^^^^^^^^^
Always put the opening brace on the same line as the previous statement.
Bad Example
.. code-block:: javascript
function func()
{
return
{
"name": "Batman"
};
}
Good Example
.. code-block:: javascript
function func() {
return {
"name": "Batman"
};
}
Closing Brace Location
^^^^^^^^^^^^^^^^^^^^^^
The closing brace should be on the same indent level as the original function call.
Bad Example
.. code-block:: javascript
function func() {
return {
"name": "Batman"
};
}
Good Example
.. code-block:: javascript
function func() {
return {
"name": "Batman"
};
}
Function Declaration Location
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Non anonymous functions should be declared before use.
Bad Example
.. code-block:: javascript
return {
"namedFunction": function namedFunction() {
return;
}
};
Good Example
.. code-block:: javascript
function namedFunction() {
return;
}
return {
"namedFunction": namedFunction
};
Naming Conventions
------------------
Naming conventions include generic patterns for setting names and identifiers throughout a script.
Constructors
^^^^^^^^^^^^
Constructor functions (called with the ``new`` statement) should always start with a capital letter:
.. code-block:: javascript
// bad example
var test = new application();
// good example
var test = new Application();
Methods/Functions
^^^^^^^^^^^^^^^^^
A method/function should always start with a small letter.
.. code-block:: javascript
// bad example
function MyFunction() {...}
// good example
function myFunction() {...}
TitleCase, camelCase
^^^^^^^^^^^^^^^^^^^^
Follow the camel case convention, typing the words in lower-case, only capitalizing the first letter in each word.
.. code-block:: javascript
// Good example constructor = TitleCase
var test = new PrototypeApplication();
// Bad example constructor
var test = new PROTOTYPEAPPLICATION();
// Good example functions/methods = camelCase
myFunction();
calculateArea();
// Bad example functions/methods
MyFunction();
CalculateArea();
Variables
^^^^^^^^^
Variable names with multiple words should always use an underscore between them.
.. code-block:: javascript
// bad example
var deliveryNote = 1;
// good example
var delivery_note = 1;
Confusing variable names should end with the variable type.
.. code-block:: javascript
// implicit type
var my_callback = doSomething();
var Person = require("./person");
// confusing names + var type
var do_something_function = doSomething.bind(context);
var value_list = getObjectOrArray();
// value_list can be an object which can be cast into an array
To use camelCase, when sometimes it is not possible to declare a function
directly, the function variable name should match some pattern which shows
that it is a function.
.. code-block:: javascript
// good example
var doSomethingFunction = function () { ... };
// or
var tool = {"doSomething": function () { ... }};
// bad example
var doSomething = function () { ... };
Element Classes and IDs
^^^^^^^^^^^^^^^^^^^^^^^
JavaScript can access elements by their ID attribute and class names. When
assigning IDs and class names with multiple words, these should also be
separated by an underscore (same as variables).
Example
.. code-block:: javascript
// bad example
test.setAttribute("id", "uniqueIdentifier");
// good example
test.setAttribute("id", "unique_identifier");
Discuss - checked with jQuery UI/jQuery Mobile, they don't use written name conventions, only
* events names should fit their purpose (pageChange for changing a page)
* element classes use “-” like in ui-shadow
* "ui" should not be used by third party developers
* variables and events use lower camel-case like pageChange and activePage
Underscore Private Methods
^^^^^^^^^^^^^^^^^^^^^^^^^^
Private methods should use a leading underscore to separate them from public methods (although this does not technically make a method private).
Good Example
.. code-block:: javascript
var person = {
"getName": function () {
return this._getFirst() + " " + this._getLast();
},
"_getFirst": function () {
// ...
},
"_getLast": function () {
// ...
}
};
Bad Example
.. code-block:: javascript
var person = {
"getName": function () {
return this.getFirst() + " " + this.getLast();
},
// private function
"getFirst": function () {
// ...
}
};
No Abbreviations
^^^^^^^^^^^^^^^^
Abbreviations should not be used to avoid confusion.
Good Example
.. code-block:: javascript
// delivery note
var delivery_note = 1;
Bad Example
.. code-block:: javascript
// delivery note
var del_note = 1;
No Plurals
^^^^^^^^^^
Plurals should not be used as variable names.
.. code-block:: javascript
// good example
var delivery_note_list = ["one", "two"];
// bad example
var delivery_notes = ["one", "two"];
Use Comments
^^^^^^^^^^^^
Comments should be used within reason but include enough information so that a
reader can get a first grasp of what a part of code is supposed to do.
Good Example
.. code-block:: javascript
var person = {
// returns full name string
"getName": function () {
return this._getFirst() + " " + this._getLast();
}
};
Bad Example
.. code-block:: javascript
var person = {
"getName": function () {
return this._getFirst() + " " + this._getLast();
}
};
Documentation
^^^^^^^^^^^^^
You can use `YUIDoc <http://yuilibrary.com/projects/yuidoc>`_ and its custom comment
tags together with Node.js to generate the documentation from the script file
itself. Comments should look like this:
Good Example
.. code-block:: javascript
/**
* Reverse a string
*
* @param {String} input_string String to reverse
* @return {String} The reversed string
*/
function reverse(input_string) {
// ...
return output_string;
};
Bad Example
.. code-block:: javascript
function reverse(input_string) {
// ...
return output_string;
};
Additional Readings
-------------------
Resources, additional reading materials and links:
* `JavaScript Patterns <http://shop.oreilly.com/product/9780596806767.do>`_, main resource used.
* `JSLint <http://www.jslint.com/>`_, code quality tool.
* `JSLint Error Explanations <http://jslinterrors.com/>`_, a useful reference.
* `YUIDoc <http://yuilibrary.com/projects/yuidoc>`_, generate documentation from code.
......@@ -50,6 +50,7 @@ constants.http_status_text = {
"204": "No Content",
"205": "Reset Content",
"206": "Partial Content",
"304": "Not Modified",
"400": "Bad Request",
"401": "Unauthorized",
"402": "Payment Required",
......@@ -83,6 +84,7 @@ constants.http_status_text = {
"No Content": "No Content",
"Reset Content": "Reset Content",
"Partial Content": "Partial Content",
"Not Modified": "Not Modified",
"Bad Request": "Bad Request",
"Unauthorized": "Unauthorized",
"Payment Required": "Payment Required",
......@@ -115,6 +117,7 @@ constants.http_status_text = {
"no_content": "No Content",
"reset_content": "Reset Content",
"partial_content": "Partial Content",
"not_modified": "Not Modified",
"bad_request": "Bad Request",
"unauthorized": "Unauthorized",
"payment_required": "Payment Required",
......@@ -159,6 +162,7 @@ constants.http_status = {
"204": 204,
"205": 205,
"206": 206,
"304": 304,
"400": 400,
"401": 401,
"402": 402,
......@@ -192,6 +196,7 @@ constants.http_status = {
"No Content": 204,
"Reset Content": 205,
"Partial Content": 206,
"Not Modified": 304,
"Bad Request": 400,
"Unauthorized": 401,
"Payment Required": 402,
......@@ -224,6 +229,7 @@ constants.http_status = {
"no_content": 204,
"reset_content": 205,
"partial_content": 206,
"not_modified": 304,
"bad_request": 400,
"unauthorized": 401,
"payment_required": 402,
......@@ -268,6 +274,7 @@ constants.http_action = {
"204": "success",
"205": "success",
"206": "success",
"304": "success",
"400": "error",
"401": "error",
"402": "error",
......@@ -301,6 +308,7 @@ constants.http_action = {
"No Content": "success",
"Reset Content": "success",
"Partial Content": "success",
"Not Modified": "success",
"Bad Request": "error",
"Unauthorized": "error",
"Payment Required": "error",
......@@ -333,6 +341,7 @@ constants.http_action = {
"no_content": "success",
"reset_content": "success",
"partial_content": "success",
"not_modified": "success",
"bad_request": "error",
"unauthorized": "error",
"payment_required": "error",
......
This source diff could not be displayed because it is too large. You can view the blob instead.
//! moment.js
//! version : 2.5.0
//! authors : Tim Wood, Iskren Chernev, Moment.js contributors
//! license : MIT
//! momentjs.com
(function (undefined) {
/************************************
Constants
************************************/
var moment,
VERSION = "2.5.0",
global = this,
round = Math.round,
i,
YEAR = 0,
MONTH = 1,
DATE = 2,
HOUR = 3,
MINUTE = 4,
SECOND = 5,
MILLISECOND = 6,
// internal storage for language config files
languages = {},
// check for nodeJS
hasModule = (typeof module !== 'undefined' && module.exports && typeof require !== 'undefined'),
// ASP.NET json date format regex
aspNetJsonRegex = /^\/?Date\((\-?\d+)/i,
aspNetTimeSpanJsonRegex = /(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/,
// from http://docs.closure-library.googlecode.com/git/closure_goog_date_date.js.source.html
// somewhat more in line with 4.4.3.2 2004 spec, but allows decimal anywhere
isoDurationRegex = /^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/,
// format tokens
formattingTokens = /(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g,
localFormattingTokens = /(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,
// parsing token regexes
parseTokenOneOrTwoDigits = /\d\d?/, // 0 - 99
parseTokenOneToThreeDigits = /\d{1,3}/, // 0 - 999
parseTokenOneToFourDigits = /\d{1,4}/, // 0 - 9999
parseTokenOneToSixDigits = /[+\-]?\d{1,6}/, // -999,999 - 999,999
parseTokenDigits = /\d+/, // nonzero number of digits
parseTokenWord = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i, // any word (or two) characters or numbers including two/three word month in arabic.
parseTokenTimezone = /Z|[\+\-]\d\d:?\d\d/gi, // +00:00 -00:00 +0000 -0000 or Z
parseTokenT = /T/i, // T (ISO separator)
parseTokenTimestampMs = /[\+\-]?\d+(\.\d{1,3})?/, // 123456789 123456789.123
//strict parsing regexes
parseTokenOneDigit = /\d/, // 0 - 9
parseTokenTwoDigits = /\d\d/, // 00 - 99
parseTokenThreeDigits = /\d{3}/, // 000 - 999
parseTokenFourDigits = /\d{4}/, // 0000 - 9999
parseTokenSixDigits = /[+\-]?\d{6}/, // -999,999 - 999,999
// iso 8601 regex
// 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000 or +00)
isoRegex = /^\s*\d{4}-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d+)?)?)?)?([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?$/,
isoFormat = 'YYYY-MM-DDTHH:mm:ssZ',
isoDates = [
'YYYY-MM-DD',
'GGGG-[W]WW',
'GGGG-[W]WW-E',
'YYYY-DDD'
],
// iso time formats and regexes
isoTimes = [
['HH:mm:ss.SSSS', /(T| )\d\d:\d\d:\d\d\.\d{1,3}/],
['HH:mm:ss', /(T| )\d\d:\d\d:\d\d/],
['HH:mm', /(T| )\d\d:\d\d/],
['HH', /(T| )\d\d/]
],
// timezone chunker "+10:00" > ["10", "00"] or "-1530" > ["-15", "30"]
parseTimezoneChunker = /([\+\-]|\d\d)/gi,
// getter and setter names
proxyGettersAndSetters = 'Date|Hours|Minutes|Seconds|Milliseconds'.split('|'),
unitMillisecondFactors = {
'Milliseconds' : 1,
'Seconds' : 1e3,
'Minutes' : 6e4,
'Hours' : 36e5,
'Days' : 864e5,
'Months' : 2592e6,
'Years' : 31536e6
},
unitAliases = {
ms : 'millisecond',
s : 'second',
m : 'minute',
h : 'hour',
d : 'day',
D : 'date',
w : 'week',
W : 'isoWeek',
M : 'month',
y : 'year',
DDD : 'dayOfYear',
e : 'weekday',
E : 'isoWeekday',
gg: 'weekYear',
GG: 'isoWeekYear'
},
camelFunctions = {
dayofyear : 'dayOfYear',
isoweekday : 'isoWeekday',
isoweek : 'isoWeek',
weekyear : 'weekYear',
isoweekyear : 'isoWeekYear'
},
// format function strings
formatFunctions = {},
// tokens to ordinalize and pad
ordinalizeTokens = 'DDD w W M D d'.split(' '),
paddedTokens = 'M D H h m s w W'.split(' '),
formatTokenFunctions = {
M : function () {
return this.month() + 1;
},
MMM : function (format) {
return this.lang().monthsShort(this, format);
},
MMMM : function (format) {
return this.lang().months(this, format);
},
D : function () {
return this.date();
},
DDD : function () {
return this.dayOfYear();
},
d : function () {
return this.day();
},
dd : function (format) {
return this.lang().weekdaysMin(this, format);
},
ddd : function (format) {
return this.lang().weekdaysShort(this, format);
},
dddd : function (format) {
return this.lang().weekdays(this, format);
},
w : function () {
return this.week();
},
W : function () {
return this.isoWeek();
},
YY : function () {
return leftZeroFill(this.year() % 100, 2);
},
YYYY : function () {
return leftZeroFill(this.year(), 4);
},
YYYYY : function () {
return leftZeroFill(this.year(), 5);
},
YYYYYY : function () {
var y = this.year(), sign = y >= 0 ? '+' : '-';
return sign + leftZeroFill(Math.abs(y), 6);
},
gg : function () {
return leftZeroFill(this.weekYear() % 100, 2);
},
gggg : function () {
return this.weekYear();
},
ggggg : function () {
return leftZeroFill(this.weekYear(), 5);
},
GG : function () {
return leftZeroFill(this.isoWeekYear() % 100, 2);
},
GGGG : function () {
return this.isoWeekYear();
},
GGGGG : function () {
return leftZeroFill(this.isoWeekYear(), 5);
},
e : function () {
return this.weekday();
},
E : function () {
return this.isoWeekday();
},
a : function () {
return this.lang().meridiem(this.hours(), this.minutes(), true);
},
A : function () {
return this.lang().meridiem(this.hours(), this.minutes(), false);
},
H : function () {
return this.hours();
},
h : function () {
return this.hours() % 12 || 12;
},
m : function () {
return this.minutes();
},
s : function () {
return this.seconds();
},
S : function () {
return toInt(this.milliseconds() / 100);
},
SS : function () {
return leftZeroFill(toInt(this.milliseconds() / 10), 2);
},
SSS : function () {
return leftZeroFill(this.milliseconds(), 3);
},
SSSS : function () {
return leftZeroFill(this.milliseconds(), 3);
},
Z : function () {
var a = -this.zone(),
b = "+";
if (a < 0) {
a = -a;
b = "-";
}
return b + leftZeroFill(toInt(a / 60), 2) + ":" + leftZeroFill(toInt(a) % 60, 2);
},
ZZ : function () {
var a = -this.zone(),
b = "+";
if (a < 0) {
a = -a;
b = "-";
}
return b + leftZeroFill(toInt(a / 60), 2) + leftZeroFill(toInt(a) % 60, 2);
},
z : function () {
return this.zoneAbbr();
},
zz : function () {
return this.zoneName();
},
X : function () {
return this.unix();
},
Q : function () {
return this.quarter();
}
},
lists = ['months', 'monthsShort', 'weekdays', 'weekdaysShort', 'weekdaysMin'];
function padToken(func, count) {
return function (a) {
return leftZeroFill(func.call(this, a), count);
};
}
function ordinalizeToken(func, period) {
return function (a) {
return this.lang().ordinal(func.call(this, a), period);
};
}
while (ordinalizeTokens.length) {
i = ordinalizeTokens.pop();
formatTokenFunctions[i + 'o'] = ordinalizeToken(formatTokenFunctions[i], i);
}
while (paddedTokens.length) {
i = paddedTokens.pop();
formatTokenFunctions[i + i] = padToken(formatTokenFunctions[i], 2);
}
formatTokenFunctions.DDDD = padToken(formatTokenFunctions.DDD, 3);
/************************************
Constructors
************************************/
function Language() {
}
// Moment prototype object
function Moment(config) {
checkOverflow(config);
extend(this, config);
}
// Duration Constructor
function Duration(duration) {
var normalizedInput = normalizeObjectUnits(duration),
years = normalizedInput.year || 0,
months = normalizedInput.month || 0,
weeks = normalizedInput.week || 0,
days = normalizedInput.day || 0,
hours = normalizedInput.hour || 0,
minutes = normalizedInput.minute || 0,
seconds = normalizedInput.second || 0,
milliseconds = normalizedInput.millisecond || 0;
// representation for dateAddRemove
this._milliseconds = +milliseconds +
seconds * 1e3 + // 1000
minutes * 6e4 + // 1000 * 60
hours * 36e5; // 1000 * 60 * 60
// Because of dateAddRemove treats 24 hours as different from a
// day when working around DST, we need to store them separately
this._days = +days +
weeks * 7;
// It is impossible translate months into days without knowing
// which months you are are talking about, so we have to store
// it separately.
this._months = +months +
years * 12;
this._data = {};
this._bubble();
}
/************************************
Helpers
************************************/
function extend(a, b) {
for (var i in b) {
if (b.hasOwnProperty(i)) {
a[i] = b[i];
}
}
if (b.hasOwnProperty("toString")) {
a.toString = b.toString;
}
if (b.hasOwnProperty("valueOf")) {
a.valueOf = b.valueOf;
}
return a;
}
function absRound(number) {
if (number < 0) {
return Math.ceil(number);
} else {
return Math.floor(number);
}
}
// left zero fill a number
// see http://jsperf.com/left-zero-filling for performance comparison
function leftZeroFill(number, targetLength, forceSign) {
var output = Math.abs(number) + '',
sign = number >= 0;
while (output.length < targetLength) {
output = '0' + output;
}
return (sign ? (forceSign ? '+' : '') : '-') + output;
}
// helper function for _.addTime and _.subtractTime
function addOrSubtractDurationFromMoment(mom, duration, isAdding, ignoreUpdateOffset) {
var milliseconds = duration._milliseconds,
days = duration._days,
months = duration._months,
minutes,
hours;
if (milliseconds) {
mom._d.setTime(+mom._d + milliseconds * isAdding);
}
// store the minutes and hours so we can restore them
if (days || months) {
minutes = mom.minute();
hours = mom.hour();
}
if (days) {
mom.date(mom.date() + days * isAdding);
}
if (months) {
mom.month(mom.month() + months * isAdding);
}
if (milliseconds && !ignoreUpdateOffset) {
moment.updateOffset(mom);
}
// restore the minutes and hours after possibly changing dst
if (days || months) {
mom.minute(minutes);
mom.hour(hours);
}
}
// check if is an array
function isArray(input) {
return Object.prototype.toString.call(input) === '[object Array]';
}
function isDate(input) {
return Object.prototype.toString.call(input) === '[object Date]' ||
input instanceof Date;
}
// compare two arrays, return the number of differences
function compareArrays(array1, array2, dontConvert) {
var len = Math.min(array1.length, array2.length),
lengthDiff = Math.abs(array1.length - array2.length),
diffs = 0,
i;
for (i = 0; i < len; i++) {
if ((dontConvert && array1[i] !== array2[i]) ||
(!dontConvert && toInt(array1[i]) !== toInt(array2[i]))) {
diffs++;
}
}
return diffs + lengthDiff;
}
function normalizeUnits(units) {
if (units) {
var lowered = units.toLowerCase().replace(/(.)s$/, '$1');
units = unitAliases[units] || camelFunctions[lowered] || lowered;
}
return units;
}
function normalizeObjectUnits(inputObject) {
var normalizedInput = {},
normalizedProp,
prop;
for (prop in inputObject) {
if (inputObject.hasOwnProperty(prop)) {
normalizedProp = normalizeUnits(prop);
if (normalizedProp) {
normalizedInput[normalizedProp] = inputObject[prop];
}
}
}
return normalizedInput;
}
function makeList(field) {
var count, setter;
if (field.indexOf('week') === 0) {
count = 7;
setter = 'day';
}
else if (field.indexOf('month') === 0) {
count = 12;
setter = 'month';
}
else {
return;
}
moment[field] = function (format, index) {
var i, getter,
method = moment.fn._lang[field],
results = [];
if (typeof format === 'number') {
index = format;
format = undefined;
}
getter = function (i) {
var m = moment().utc().set(setter, i);
return method.call(moment.fn._lang, m, format || '');
};
if (index != null) {
return getter(index);
}
else {
for (i = 0; i < count; i++) {
results.push(getter(i));
}
return results;
}
};
}
function toInt(argumentForCoercion) {
var coercedNumber = +argumentForCoercion,
value = 0;
if (coercedNumber !== 0 && isFinite(coercedNumber)) {
if (coercedNumber >= 0) {
value = Math.floor(coercedNumber);
} else {
value = Math.ceil(coercedNumber);
}
}
return value;
}
function daysInMonth(year, month) {
return new Date(Date.UTC(year, month + 1, 0)).getUTCDate();
}
function daysInYear(year) {
return isLeapYear(year) ? 366 : 365;
}
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
}
function checkOverflow(m) {
var overflow;
if (m._a && m._pf.overflow === -2) {
overflow =
m._a[MONTH] < 0 || m._a[MONTH] > 11 ? MONTH :
m._a[DATE] < 1 || m._a[DATE] > daysInMonth(m._a[YEAR], m._a[MONTH]) ? DATE :
m._a[HOUR] < 0 || m._a[HOUR] > 23 ? HOUR :
m._a[MINUTE] < 0 || m._a[MINUTE] > 59 ? MINUTE :
m._a[SECOND] < 0 || m._a[SECOND] > 59 ? SECOND :
m._a[MILLISECOND] < 0 || m._a[MILLISECOND] > 999 ? MILLISECOND :
-1;
if (m._pf._overflowDayOfYear && (overflow < YEAR || overflow > DATE)) {
overflow = DATE;
}
m._pf.overflow = overflow;
}
}
function initializeParsingFlags(config) {
config._pf = {
empty : false,
unusedTokens : [],
unusedInput : [],
overflow : -2,
charsLeftOver : 0,
nullInput : false,
invalidMonth : null,
invalidFormat : false,
userInvalidated : false,
iso: false
};
}
function isValid(m) {
if (m._isValid == null) {
m._isValid = !isNaN(m._d.getTime()) &&
m._pf.overflow < 0 &&
!m._pf.empty &&
!m._pf.invalidMonth &&
!m._pf.nullInput &&
!m._pf.invalidFormat &&
!m._pf.userInvalidated;
if (m._strict) {
m._isValid = m._isValid &&
m._pf.charsLeftOver === 0 &&
m._pf.unusedTokens.length === 0;
}
}
return m._isValid;
}
function normalizeLanguage(key) {
return key ? key.toLowerCase().replace('_', '-') : key;
}
// Return a moment from input, that is local/utc/zone equivalent to model.
function makeAs(input, model) {
return model._isUTC ? moment(input).zone(model._offset || 0) :
moment(input).local();
}
/************************************
Languages
************************************/
extend(Language.prototype, {
set : function (config) {
var prop, i;
for (i in config) {
prop = config[i];
if (typeof prop === 'function') {
this[i] = prop;
} else {
this['_' + i] = prop;
}
}
},
_months : "January_February_March_April_May_June_July_August_September_October_November_December".split("_"),
months : function (m) {
return this._months[m.month()];
},
_monthsShort : "Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),
monthsShort : function (m) {
return this._monthsShort[m.month()];
},
monthsParse : function (monthName) {
var i, mom, regex;
if (!this._monthsParse) {
this._monthsParse = [];
}
for (i = 0; i < 12; i++) {
// make the regex if we don't have it already
if (!this._monthsParse[i]) {
mom = moment.utc([2000, i]);
regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
}
// test the regex
if (this._monthsParse[i].test(monthName)) {
return i;
}
}
},
_weekdays : "Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),
weekdays : function (m) {
return this._weekdays[m.day()];
},
_weekdaysShort : "Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),
weekdaysShort : function (m) {
return this._weekdaysShort[m.day()];
},
_weekdaysMin : "Su_Mo_Tu_We_Th_Fr_Sa".split("_"),
weekdaysMin : function (m) {
return this._weekdaysMin[m.day()];
},
weekdaysParse : function (weekdayName) {
var i, mom, regex;
if (!this._weekdaysParse) {
this._weekdaysParse = [];
}
for (i = 0; i < 7; i++) {
// make the regex if we don't have it already
if (!this._weekdaysParse[i]) {
mom = moment([2000, 1]).day(i);
regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, '');
this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i');
}
// test the regex
if (this._weekdaysParse[i].test(weekdayName)) {
return i;
}
}
},
_longDateFormat : {
LT : "h:mm A",
L : "MM/DD/YYYY",
LL : "MMMM D YYYY",
LLL : "MMMM D YYYY LT",
LLLL : "dddd, MMMM D YYYY LT"
},
longDateFormat : function (key) {
var output = this._longDateFormat[key];
if (!output && this._longDateFormat[key.toUpperCase()]) {
output = this._longDateFormat[key.toUpperCase()].replace(/MMMM|MM|DD|dddd/g, function (val) {
return val.slice(1);
});
this._longDateFormat[key] = output;
}
return output;
},
isPM : function (input) {
// IE8 Quirks Mode & IE7 Standards Mode do not allow accessing strings like arrays
// Using charAt should be more compatible.
return ((input + '').toLowerCase().charAt(0) === 'p');
},
_meridiemParse : /[ap]\.?m?\.?/i,
meridiem : function (hours, minutes, isLower) {
if (hours > 11) {
return isLower ? 'pm' : 'PM';
} else {
return isLower ? 'am' : 'AM';
}
},
_calendar : {
sameDay : '[Today at] LT',
nextDay : '[Tomorrow at] LT',
nextWeek : 'dddd [at] LT',
lastDay : '[Yesterday at] LT',
lastWeek : '[Last] dddd [at] LT',
sameElse : 'L'
},
calendar : function (key, mom) {
var output = this._calendar[key];
return typeof output === 'function' ? output.apply(mom) : output;
},
_relativeTime : {
future : "in %s",
past : "%s ago",
s : "a few seconds",
m : "a minute",
mm : "%d minutes",
h : "an hour",
hh : "%d hours",
d : "a day",
dd : "%d days",
M : "a month",
MM : "%d months",
y : "a year",
yy : "%d years"
},
relativeTime : function (number, withoutSuffix, string, isFuture) {
var output = this._relativeTime[string];
return (typeof output === 'function') ?
output(number, withoutSuffix, string, isFuture) :
output.replace(/%d/i, number);
},
pastFuture : function (diff, output) {
var format = this._relativeTime[diff > 0 ? 'future' : 'past'];
return typeof format === 'function' ? format(output) : format.replace(/%s/i, output);
},
ordinal : function (number) {
return this._ordinal.replace("%d", number);
},
_ordinal : "%d",
preparse : function (string) {
return string;
},
postformat : function (string) {
return string;
},
week : function (mom) {
return weekOfYear(mom, this._week.dow, this._week.doy).week;
},
_week : {
dow : 0, // Sunday is the first day of the week.
doy : 6 // The week that contains Jan 1st is the first week of the year.
},
_invalidDate: 'Invalid date',
invalidDate: function () {
return this._invalidDate;
}
});
// Loads a language definition into the `languages` cache. The function
// takes a key and optionally values. If not in the browser and no values
// are provided, it will load the language file module. As a convenience,
// this function also returns the language values.
function loadLang(key, values) {
values.abbr = key;
if (!languages[key]) {
languages[key] = new Language();
}
languages[key].set(values);
return languages[key];
}
// Remove a language from the `languages` cache. Mostly useful in tests.
function unloadLang(key) {
delete languages[key];
}
// Determines which language definition to use and returns it.
//
// With no parameters, it will return the global language. If you
// pass in a language key, such as 'en', it will return the
// definition for 'en', so long as 'en' has already been loaded using
// moment.lang.
function getLangDefinition(key) {
var i = 0, j, lang, next, split,
get = function (k) {
if (!languages[k] && hasModule) {
try {
require('./lang/' + k);
} catch (e) { }
}
return languages[k];
};
if (!key) {
return moment.fn._lang;
}
if (!isArray(key)) {
//short-circuit everything else
lang = get(key);
if (lang) {
return lang;
}
key = [key];
}
//pick the language from the array
//try ['en-au', 'en-gb'] as 'en-au', 'en-gb', 'en', as in move through the list trying each
//substring from most specific to least, but move to the next array item if it's a more specific variant than the current root
while (i < key.length) {
split = normalizeLanguage(key[i]).split('-');
j = split.length;
next = normalizeLanguage(key[i + 1]);
next = next ? next.split('-') : null;
while (j > 0) {
lang = get(split.slice(0, j).join('-'));
if (lang) {
return lang;
}
if (next && next.length >= j && compareArrays(split, next, true) >= j - 1) {
//the next array item is better than a shallower substring of this one
break;
}
j--;
}
i++;
}
return moment.fn._lang;
}
/************************************
Formatting
************************************/
function removeFormattingTokens(input) {
if (input.match(/\[[\s\S]/)) {
return input.replace(/^\[|\]$/g, "");
}
return input.replace(/\\/g, "");
}
function makeFormatFunction(format) {
var array = format.match(formattingTokens), i, length;
for (i = 0, length = array.length; i < length; i++) {
if (formatTokenFunctions[array[i]]) {
array[i] = formatTokenFunctions[array[i]];
} else {
array[i] = removeFormattingTokens(array[i]);
}
}
return function (mom) {
var output = "";
for (i = 0; i < length; i++) {
output += array[i] instanceof Function ? array[i].call(mom, format) : array[i];
}
return output;
};
}
// format date using native date object
function formatMoment(m, format) {
if (!m.isValid()) {
return m.lang().invalidDate();
}
format = expandFormat(format, m.lang());
if (!formatFunctions[format]) {
formatFunctions[format] = makeFormatFunction(format);
}
return formatFunctions[format](m);
}
function expandFormat(format, lang) {
var i = 5;
function replaceLongDateFormatTokens(input) {
return lang.longDateFormat(input) || input;
}
localFormattingTokens.lastIndex = 0;
while (i >= 0 && localFormattingTokens.test(format)) {
format = format.replace(localFormattingTokens, replaceLongDateFormatTokens);
localFormattingTokens.lastIndex = 0;
i -= 1;
}
return format;
}
/************************************
Parsing
************************************/
// get the regex to find the next token
function getParseRegexForToken(token, config) {
var a, strict = config._strict;
switch (token) {
case 'DDDD':
return parseTokenThreeDigits;
case 'YYYY':
case 'GGGG':
case 'gggg':
return strict ? parseTokenFourDigits : parseTokenOneToFourDigits;
case 'YYYYYY':
case 'YYYYY':
case 'GGGGG':
case 'ggggg':
return strict ? parseTokenSixDigits : parseTokenOneToSixDigits;
case 'S':
if (strict) { return parseTokenOneDigit; }
/* falls through */
case 'SS':
if (strict) { return parseTokenTwoDigits; }
/* falls through */
case 'SSS':
case 'DDD':
return strict ? parseTokenThreeDigits : parseTokenOneToThreeDigits;
case 'MMM':
case 'MMMM':
case 'dd':
case 'ddd':
case 'dddd':
return parseTokenWord;
case 'a':
case 'A':
return getLangDefinition(config._l)._meridiemParse;
case 'X':
return parseTokenTimestampMs;
case 'Z':
case 'ZZ':
return parseTokenTimezone;
case 'T':
return parseTokenT;
case 'SSSS':
return parseTokenDigits;
case 'MM':
case 'DD':
case 'YY':
case 'GG':
case 'gg':
case 'HH':
case 'hh':
case 'mm':
case 'ss':
case 'ww':
case 'WW':
return strict ? parseTokenTwoDigits : parseTokenOneOrTwoDigits;
case 'M':
case 'D':
case 'd':
case 'H':
case 'h':
case 'm':
case 's':
case 'w':
case 'W':
case 'e':
case 'E':
return strict ? parseTokenOneDigit : parseTokenOneOrTwoDigits;
default :
a = new RegExp(regexpEscape(unescapeFormat(token.replace('\\', '')), "i"));
return a;
}
}
function timezoneMinutesFromString(string) {
string = string || "";
var possibleTzMatches = (string.match(parseTokenTimezone) || []),
tzChunk = possibleTzMatches[possibleTzMatches.length - 1] || [],
parts = (tzChunk + '').match(parseTimezoneChunker) || ['-', 0, 0],
minutes = +(parts[1] * 60) + toInt(parts[2]);
return parts[0] === '+' ? -minutes : minutes;
}
// function to convert string input to date
function addTimeToArrayFromToken(token, input, config) {
var a, datePartArray = config._a;
switch (token) {
// MONTH
case 'M' : // fall through to MM
case 'MM' :
if (input != null) {
datePartArray[MONTH] = toInt(input) - 1;
}
break;
case 'MMM' : // fall through to MMMM
case 'MMMM' :
a = getLangDefinition(config._l).monthsParse(input);
// if we didn't find a month name, mark the date as invalid.
if (a != null) {
datePartArray[MONTH] = a;
} else {
config._pf.invalidMonth = input;
}
break;
// DAY OF MONTH
case 'D' : // fall through to DD
case 'DD' :
if (input != null) {
datePartArray[DATE] = toInt(input);
}
break;
// DAY OF YEAR
case 'DDD' : // fall through to DDDD
case 'DDDD' :
if (input != null) {
config._dayOfYear = toInt(input);
}
break;
// YEAR
case 'YY' :
datePartArray[YEAR] = toInt(input) + (toInt(input) > 68 ? 1900 : 2000);
break;
case 'YYYY' :
case 'YYYYY' :
case 'YYYYYY' :
datePartArray[YEAR] = toInt(input);
break;
// AM / PM
case 'a' : // fall through to A
case 'A' :
config._isPm = getLangDefinition(config._l).isPM(input);
break;
// 24 HOUR
case 'H' : // fall through to hh
case 'HH' : // fall through to hh
case 'h' : // fall through to hh
case 'hh' :
datePartArray[HOUR] = toInt(input);
break;
// MINUTE
case 'm' : // fall through to mm
case 'mm' :
datePartArray[MINUTE] = toInt(input);
break;
// SECOND
case 's' : // fall through to ss
case 'ss' :
datePartArray[SECOND] = toInt(input);
break;
// MILLISECOND
case 'S' :
case 'SS' :
case 'SSS' :
case 'SSSS' :
datePartArray[MILLISECOND] = toInt(('0.' + input) * 1000);
break;
// UNIX TIMESTAMP WITH MS
case 'X':
config._d = new Date(parseFloat(input) * 1000);
break;
// TIMEZONE
case 'Z' : // fall through to ZZ
case 'ZZ' :
config._useUTC = true;
config._tzm = timezoneMinutesFromString(input);
break;
case 'w':
case 'ww':
case 'W':
case 'WW':
case 'd':
case 'dd':
case 'ddd':
case 'dddd':
case 'e':
case 'E':
token = token.substr(0, 1);
/* falls through */
case 'gg':
case 'gggg':
case 'GG':
case 'GGGG':
case 'GGGGG':
token = token.substr(0, 2);
if (input) {
config._w = config._w || {};
config._w[token] = input;
}
break;
}
}
// convert an array to a date.
// the array should mirror the parameters below
// note: all values past the year are optional and will default to the lowest possible value.
// [year, month, day , hour, minute, second, millisecond]
function dateFromConfig(config) {
var i, date, input = [], currentDate,
yearToUse, fixYear, w, temp, lang, weekday, week;
if (config._d) {
return;
}
currentDate = currentDateArray(config);
//compute day of the year from weeks and weekdays
if (config._w && config._a[DATE] == null && config._a[MONTH] == null) {
fixYear = function (val) {
var int_val = parseInt(val, 10);
return val ?
(val.length < 3 ? (int_val > 68 ? 1900 + int_val : 2000 + int_val) : int_val) :
(config._a[YEAR] == null ? moment().weekYear() : config._a[YEAR]);
};
w = config._w;
if (w.GG != null || w.W != null || w.E != null) {
temp = dayOfYearFromWeeks(fixYear(w.GG), w.W || 1, w.E, 4, 1);
}
else {
lang = getLangDefinition(config._l);
weekday = w.d != null ? parseWeekday(w.d, lang) :
(w.e != null ? parseInt(w.e, 10) + lang._week.dow : 0);
week = parseInt(w.w, 10) || 1;
//if we're parsing 'd', then the low day numbers may be next week
if (w.d != null && weekday < lang._week.dow) {
week++;
}
temp = dayOfYearFromWeeks(fixYear(w.gg), week, weekday, lang._week.doy, lang._week.dow);
}
config._a[YEAR] = temp.year;
config._dayOfYear = temp.dayOfYear;
}
//if the day of the year is set, figure out what it is
if (config._dayOfYear) {
yearToUse = config._a[YEAR] == null ? currentDate[YEAR] : config._a[YEAR];
if (config._dayOfYear > daysInYear(yearToUse)) {
config._pf._overflowDayOfYear = true;
}
date = makeUTCDate(yearToUse, 0, config._dayOfYear);
config._a[MONTH] = date.getUTCMonth();
config._a[DATE] = date.getUTCDate();
}
// Default to current date.
// * if no year, month, day of month are given, default to today
// * if day of month is given, default month and year
// * if month is given, default only year
// * if year is given, don't default anything
for (i = 0; i < 3 && config._a[i] == null; ++i) {
config._a[i] = input[i] = currentDate[i];
}
// Zero out whatever was not defaulted, including time
for (; i < 7; i++) {
config._a[i] = input[i] = (config._a[i] == null) ? (i === 2 ? 1 : 0) : config._a[i];
}
// add the offsets to the time to be parsed so that we can have a clean array for checking isValid
input[HOUR] += toInt((config._tzm || 0) / 60);
input[MINUTE] += toInt((config._tzm || 0) % 60);
config._d = (config._useUTC ? makeUTCDate : makeDate).apply(null, input);
}
function dateFromObject(config) {
var normalizedInput;
if (config._d) {
return;
}
normalizedInput = normalizeObjectUnits(config._i);
config._a = [
normalizedInput.year,
normalizedInput.month,
normalizedInput.day,
normalizedInput.hour,
normalizedInput.minute,
normalizedInput.second,
normalizedInput.millisecond
];
dateFromConfig(config);
}
function currentDateArray(config) {
var now = new Date();
if (config._useUTC) {
return [
now.getUTCFullYear(),
now.getUTCMonth(),
now.getUTCDate()
];
} else {
return [now.getFullYear(), now.getMonth(), now.getDate()];
}
}
// date from string and format string
function makeDateFromStringAndFormat(config) {
config._a = [];
config._pf.empty = true;
// This array is used to make a Date, either with `new Date` or `Date.UTC`
var lang = getLangDefinition(config._l),
string = '' + config._i,
i, parsedInput, tokens, token, skipped,
stringLength = string.length,
totalParsedInputLength = 0;
tokens = expandFormat(config._f, lang).match(formattingTokens) || [];
for (i = 0; i < tokens.length; i++) {
token = tokens[i];
parsedInput = (string.match(getParseRegexForToken(token, config)) || [])[0];
if (parsedInput) {
skipped = string.substr(0, string.indexOf(parsedInput));
if (skipped.length > 0) {
config._pf.unusedInput.push(skipped);
}
string = string.slice(string.indexOf(parsedInput) + parsedInput.length);
totalParsedInputLength += parsedInput.length;
}
// don't parse if it's not a known token
if (formatTokenFunctions[token]) {
if (parsedInput) {
config._pf.empty = false;
}
else {
config._pf.unusedTokens.push(token);
}
addTimeToArrayFromToken(token, parsedInput, config);
}
else if (config._strict && !parsedInput) {
config._pf.unusedTokens.push(token);
}
}
// add remaining unparsed input length to the string
config._pf.charsLeftOver = stringLength - totalParsedInputLength;
if (string.length > 0) {
config._pf.unusedInput.push(string);
}
// handle am pm
if (config._isPm && config._a[HOUR] < 12) {
config._a[HOUR] += 12;
}
// if is 12 am, change hours to 0
if (config._isPm === false && config._a[HOUR] === 12) {
config._a[HOUR] = 0;
}
dateFromConfig(config);
checkOverflow(config);
}
function unescapeFormat(s) {
return s.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function (matched, p1, p2, p3, p4) {
return p1 || p2 || p3 || p4;
});
}
// Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
function regexpEscape(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
// date from string and array of format strings
function makeDateFromStringAndArray(config) {
var tempConfig,
bestMoment,
scoreToBeat,
i,
currentScore;
if (config._f.length === 0) {
config._pf.invalidFormat = true;
config._d = new Date(NaN);
return;
}
for (i = 0; i < config._f.length; i++) {
currentScore = 0;
tempConfig = extend({}, config);
initializeParsingFlags(tempConfig);
tempConfig._f = config._f[i];
makeDateFromStringAndFormat(tempConfig);
if (!isValid(tempConfig)) {
continue;
}
// if there is any input that was not parsed add a penalty for that format
currentScore += tempConfig._pf.charsLeftOver;
//or tokens
currentScore += tempConfig._pf.unusedTokens.length * 10;
tempConfig._pf.score = currentScore;
if (scoreToBeat == null || currentScore < scoreToBeat) {
scoreToBeat = currentScore;
bestMoment = tempConfig;
}
}
extend(config, bestMoment || tempConfig);
}
// date from iso format
function makeDateFromString(config) {
var i,
string = config._i,
match = isoRegex.exec(string);
if (match) {
config._pf.iso = true;
for (i = 4; i > 0; i--) {
if (match[i]) {
// match[5] should be "T" or undefined
config._f = isoDates[i - 1] + (match[6] || " ");
break;
}
}
for (i = 0; i < 4; i++) {
if (isoTimes[i][1].exec(string)) {
config._f += isoTimes[i][0];
break;
}
}
if (string.match(parseTokenTimezone)) {
config._f += "Z";
}
makeDateFromStringAndFormat(config);
}
else {
config._d = new Date(string);
}
}
function makeDateFromInput(config) {
var input = config._i,
matched = aspNetJsonRegex.exec(input);
if (input === undefined) {
config._d = new Date();
} else if (matched) {
config._d = new Date(+matched[1]);
} else if (typeof input === 'string') {
makeDateFromString(config);
} else if (isArray(input)) {
config._a = input.slice(0);
dateFromConfig(config);
} else if (isDate(input)) {
config._d = new Date(+input);
} else if (typeof(input) === 'object') {
dateFromObject(config);
} else {
config._d = new Date(input);
}
}
function makeDate(y, m, d, h, M, s, ms) {
//can't just apply() to create a date:
//http://stackoverflow.com/questions/181348/instantiating-a-javascript-object-by-calling-prototype-constructor-apply
var date = new Date(y, m, d, h, M, s, ms);
//the date constructor doesn't accept years < 1970
if (y < 1970) {
date.setFullYear(y);
}
return date;
}
function makeUTCDate(y) {
var date = new Date(Date.UTC.apply(null, arguments));
if (y < 1970) {
date.setUTCFullYear(y);
}
return date;
}
function parseWeekday(input, language) {
if (typeof input === 'string') {
if (!isNaN(input)) {
input = parseInt(input, 10);
}
else {
input = language.weekdaysParse(input);
if (typeof input !== 'number') {
return null;
}
}
}
return input;
}
/************************************
Relative Time
************************************/
// helper function for moment.fn.from, moment.fn.fromNow, and moment.duration.fn.humanize
function substituteTimeAgo(string, number, withoutSuffix, isFuture, lang) {
return lang.relativeTime(number || 1, !!withoutSuffix, string, isFuture);
}
function relativeTime(milliseconds, withoutSuffix, lang) {
var seconds = round(Math.abs(milliseconds) / 1000),
minutes = round(seconds / 60),
hours = round(minutes / 60),
days = round(hours / 24),
years = round(days / 365),
args = seconds < 45 && ['s', seconds] ||
minutes === 1 && ['m'] ||
minutes < 45 && ['mm', minutes] ||
hours === 1 && ['h'] ||
hours < 22 && ['hh', hours] ||
days === 1 && ['d'] ||
days <= 25 && ['dd', days] ||
days <= 45 && ['M'] ||
days < 345 && ['MM', round(days / 30)] ||
years === 1 && ['y'] || ['yy', years];
args[2] = withoutSuffix;
args[3] = milliseconds > 0;
args[4] = lang;
return substituteTimeAgo.apply({}, args);
}
/************************************
Week of Year
************************************/
// firstDayOfWeek 0 = sun, 6 = sat
// the day of the week that starts the week
// (usually sunday or monday)
// firstDayOfWeekOfYear 0 = sun, 6 = sat
// the first week is the week that contains the first
// of this day of the week
// (eg. ISO weeks use thursday (4))
function weekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
var end = firstDayOfWeekOfYear - firstDayOfWeek,
daysToDayOfWeek = firstDayOfWeekOfYear - mom.day(),
adjustedMoment;
if (daysToDayOfWeek > end) {
daysToDayOfWeek -= 7;
}
if (daysToDayOfWeek < end - 7) {
daysToDayOfWeek += 7;
}
adjustedMoment = moment(mom).add('d', daysToDayOfWeek);
return {
week: Math.ceil(adjustedMoment.dayOfYear() / 7),
year: adjustedMoment.year()
};
}
//http://en.wikipedia.org/wiki/ISO_week_date#Calculating_a_date_given_the_year.2C_week_number_and_weekday
function dayOfYearFromWeeks(year, week, weekday, firstDayOfWeekOfYear, firstDayOfWeek) {
// The only solid way to create an iso date from year is to use
// a string format (Date.UTC handles only years > 1900). Don't ask why
// it doesn't need Z at the end.
var d = new Date(leftZeroFill(year, 6, true) + '-01-01').getUTCDay(),
daysToAdd, dayOfYear;
weekday = weekday != null ? weekday : firstDayOfWeek;
daysToAdd = firstDayOfWeek - d + (d > firstDayOfWeekOfYear ? 7 : 0);
dayOfYear = 7 * (week - 1) + (weekday - firstDayOfWeek) + daysToAdd + 1;
return {
year: dayOfYear > 0 ? year : year - 1,
dayOfYear: dayOfYear > 0 ? dayOfYear : daysInYear(year - 1) + dayOfYear
};
}
/************************************
Top Level Functions
************************************/
function makeMoment(config) {
var input = config._i,
format = config._f;
if (typeof config._pf === 'undefined') {
initializeParsingFlags(config);
}
if (input === null) {
return moment.invalid({nullInput: true});
}
if (typeof input === 'string') {
config._i = input = getLangDefinition().preparse(input);
}
if (moment.isMoment(input)) {
config = extend({}, input);
config._d = new Date(+input._d);
} else if (format) {
if (isArray(format)) {
makeDateFromStringAndArray(config);
} else {
makeDateFromStringAndFormat(config);
}
} else {
makeDateFromInput(config);
}
return new Moment(config);
}
moment = function (input, format, lang, strict) {
if (typeof(lang) === "boolean") {
strict = lang;
lang = undefined;
}
return makeMoment({
_i : input,
_f : format,
_l : lang,
_strict : strict,
_isUTC : false
});
};
// creating with utc
moment.utc = function (input, format, lang, strict) {
var m;
if (typeof(lang) === "boolean") {
strict = lang;
lang = undefined;
}
m = makeMoment({
_useUTC : true,
_isUTC : true,
_l : lang,
_i : input,
_f : format,
_strict : strict
}).utc();
return m;
};
// creating with unix timestamp (in seconds)
moment.unix = function (input) {
return moment(input * 1000);
};
// duration
moment.duration = function (input, key) {
var duration = input,
// matching against regexp is expensive, do it on demand
match = null,
sign,
ret,
parseIso;
if (moment.isDuration(input)) {
duration = {
ms: input._milliseconds,
d: input._days,
M: input._months
};
} else if (typeof input === 'number') {
duration = {};
if (key) {
duration[key] = input;
} else {
duration.milliseconds = input;
}
} else if (!!(match = aspNetTimeSpanJsonRegex.exec(input))) {
sign = (match[1] === "-") ? -1 : 1;
duration = {
y: 0,
d: toInt(match[DATE]) * sign,
h: toInt(match[HOUR]) * sign,
m: toInt(match[MINUTE]) * sign,
s: toInt(match[SECOND]) * sign,
ms: toInt(match[MILLISECOND]) * sign
};
} else if (!!(match = isoDurationRegex.exec(input))) {
sign = (match[1] === "-") ? -1 : 1;
parseIso = function (inp) {
// We'd normally use ~~inp for this, but unfortunately it also
// converts floats to ints.
// inp may be undefined, so careful calling replace on it.
var res = inp && parseFloat(inp.replace(',', '.'));
// apply sign while we're at it
return (isNaN(res) ? 0 : res) * sign;
};
duration = {
y: parseIso(match[2]),
M: parseIso(match[3]),
d: parseIso(match[4]),
h: parseIso(match[5]),
m: parseIso(match[6]),
s: parseIso(match[7]),
w: parseIso(match[8])
};
}
ret = new Duration(duration);
if (moment.isDuration(input) && input.hasOwnProperty('_lang')) {
ret._lang = input._lang;
}
return ret;
};
// version number
moment.version = VERSION;
// default format
moment.defaultFormat = isoFormat;
// This function will be called whenever a moment is mutated.
// It is intended to keep the offset in sync with the timezone.
moment.updateOffset = function () {};
// This function will load languages and then set the global language. If
// no arguments are passed in, it will simply return the current global
// language key.
moment.lang = function (key, values) {
var r;
if (!key) {
return moment.fn._lang._abbr;
}
if (values) {
loadLang(normalizeLanguage(key), values);
} else if (values === null) {
unloadLang(key);
key = 'en';
} else if (!languages[key]) {
getLangDefinition(key);
}
r = moment.duration.fn._lang = moment.fn._lang = getLangDefinition(key);
return r._abbr;
};
// returns language data
moment.langData = function (key) {
if (key && key._lang && key._lang._abbr) {
key = key._lang._abbr;
}
return getLangDefinition(key);
};
// compare moment object
moment.isMoment = function (obj) {
return obj instanceof Moment;
};
// for typechecking Duration objects
moment.isDuration = function (obj) {
return obj instanceof Duration;
};
for (i = lists.length - 1; i >= 0; --i) {
makeList(lists[i]);
}
moment.normalizeUnits = function (units) {
return normalizeUnits(units);
};
moment.invalid = function (flags) {
var m = moment.utc(NaN);
if (flags != null) {
extend(m._pf, flags);
}
else {
m._pf.userInvalidated = true;
}
return m;
};
moment.parseZone = function (input) {
return moment(input).parseZone();
};
/************************************
Moment Prototype
************************************/
extend(moment.fn = Moment.prototype, {
clone : function () {
return moment(this);
},
valueOf : function () {
return +this._d + ((this._offset || 0) * 60000);
},
unix : function () {
return Math.floor(+this / 1000);
},
toString : function () {
return this.clone().lang('en').format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ");
},
toDate : function () {
return this._offset ? new Date(+this) : this._d;
},
toISOString : function () {
var m = moment(this).utc();
if (0 < m.year() && m.year() <= 9999) {
return formatMoment(m, 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
} else {
return formatMoment(m, 'YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]');
}
},
toArray : function () {
var m = this;
return [
m.year(),
m.month(),
m.date(),
m.hours(),
m.minutes(),
m.seconds(),
m.milliseconds()
];
},
isValid : function () {
return isValid(this);
},
isDSTShifted : function () {
if (this._a) {
return this.isValid() && compareArrays(this._a, (this._isUTC ? moment.utc(this._a) : moment(this._a)).toArray()) > 0;
}
return false;
},
parsingFlags : function () {
return extend({}, this._pf);
},
invalidAt: function () {
return this._pf.overflow;
},
utc : function () {
return this.zone(0);
},
local : function () {
this.zone(0);
this._isUTC = false;
return this;
},
format : function (inputString) {
var output = formatMoment(this, inputString || moment.defaultFormat);
return this.lang().postformat(output);
},
add : function (input, val) {
var dur;
// switch args to support add('s', 1) and add(1, 's')
if (typeof input === 'string') {
dur = moment.duration(+val, input);
} else {
dur = moment.duration(input, val);
}
addOrSubtractDurationFromMoment(this, dur, 1);
return this;
},
subtract : function (input, val) {
var dur;
// switch args to support subtract('s', 1) and subtract(1, 's')
if (typeof input === 'string') {
dur = moment.duration(+val, input);
} else {
dur = moment.duration(input, val);
}
addOrSubtractDurationFromMoment(this, dur, -1);
return this;
},
diff : function (input, units, asFloat) {
var that = makeAs(input, this),
zoneDiff = (this.zone() - that.zone()) * 6e4,
diff, output;
units = normalizeUnits(units);
if (units === 'year' || units === 'month') {
// average number of days in the months in the given dates
diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2
// difference in months
output = ((this.year() - that.year()) * 12) + (this.month() - that.month());
// adjust by taking difference in days, average number of days
// and dst in the given months.
output += ((this - moment(this).startOf('month')) -
(that - moment(that).startOf('month'))) / diff;
// same as above but with zones, to negate all dst
output -= ((this.zone() - moment(this).startOf('month').zone()) -
(that.zone() - moment(that).startOf('month').zone())) * 6e4 / diff;
if (units === 'year') {
output = output / 12;
}
} else {
diff = (this - that);
output = units === 'second' ? diff / 1e3 : // 1000
units === 'minute' ? diff / 6e4 : // 1000 * 60
units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60
units === 'day' ? (diff - zoneDiff) / 864e5 : // 1000 * 60 * 60 * 24, negate dst
units === 'week' ? (diff - zoneDiff) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst
diff;
}
return asFloat ? output : absRound(output);
},
from : function (time, withoutSuffix) {
return moment.duration(this.diff(time)).lang(this.lang()._abbr).humanize(!withoutSuffix);
},
fromNow : function (withoutSuffix) {
return this.from(moment(), withoutSuffix);
},
calendar : function () {
// We want to compare the start of today, vs this.
// Getting start-of-today depends on whether we're zone'd or not.
var sod = makeAs(moment(), this).startOf('day'),
diff = this.diff(sod, 'days', true),
format = diff < -6 ? 'sameElse' :
diff < -1 ? 'lastWeek' :
diff < 0 ? 'lastDay' :
diff < 1 ? 'sameDay' :
diff < 2 ? 'nextDay' :
diff < 7 ? 'nextWeek' : 'sameElse';
return this.format(this.lang().calendar(format, this));
},
isLeapYear : function () {
return isLeapYear(this.year());
},
isDST : function () {
return (this.zone() < this.clone().month(0).zone() ||
this.zone() < this.clone().month(5).zone());
},
day : function (input) {
var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
if (input != null) {
input = parseWeekday(input, this.lang());
return this.add({ d : input - day });
} else {
return day;
}
},
month : function (input) {
var utc = this._isUTC ? 'UTC' : '',
dayOfMonth;
if (input != null) {
if (typeof input === 'string') {
input = this.lang().monthsParse(input);
if (typeof input !== 'number') {
return this;
}
}
dayOfMonth = this.date();
this.date(1);
this._d['set' + utc + 'Month'](input);
this.date(Math.min(dayOfMonth, this.daysInMonth()));
moment.updateOffset(this);
return this;
} else {
return this._d['get' + utc + 'Month']();
}
},
startOf: function (units) {
units = normalizeUnits(units);
// the following switch intentionally omits break keywords
// to utilize falling through the cases.
switch (units) {
case 'year':
this.month(0);
/* falls through */
case 'month':
this.date(1);
/* falls through */
case 'week':
case 'isoWeek':
case 'day':
this.hours(0);
/* falls through */
case 'hour':
this.minutes(0);
/* falls through */
case 'minute':
this.seconds(0);
/* falls through */
case 'second':
this.milliseconds(0);
/* falls through */
}
// weeks are a special case
if (units === 'week') {
this.weekday(0);
} else if (units === 'isoWeek') {
this.isoWeekday(1);
}
return this;
},
endOf: function (units) {
units = normalizeUnits(units);
return this.startOf(units).add((units === 'isoWeek' ? 'week' : units), 1).subtract('ms', 1);
},
isAfter: function (input, units) {
units = typeof units !== 'undefined' ? units : 'millisecond';
return +this.clone().startOf(units) > +moment(input).startOf(units);
},
isBefore: function (input, units) {
units = typeof units !== 'undefined' ? units : 'millisecond';
return +this.clone().startOf(units) < +moment(input).startOf(units);
},
isSame: function (input, units) {
units = units || 'ms';
return +this.clone().startOf(units) === +makeAs(input, this).startOf(units);
},
min: function (other) {
other = moment.apply(null, arguments);
return other < this ? this : other;
},
max: function (other) {
other = moment.apply(null, arguments);
return other > this ? this : other;
},
zone : function (input) {
var offset = this._offset || 0;
if (input != null) {
if (typeof input === "string") {
input = timezoneMinutesFromString(input);
}
if (Math.abs(input) < 16) {
input = input * 60;
}
this._offset = input;
this._isUTC = true;
if (offset !== input) {
addOrSubtractDurationFromMoment(this, moment.duration(offset - input, 'm'), 1, true);
}
} else {
return this._isUTC ? offset : this._d.getTimezoneOffset();
}
return this;
},
zoneAbbr : function () {
return this._isUTC ? "UTC" : "";
},
zoneName : function () {
return this._isUTC ? "Coordinated Universal Time" : "";
},
parseZone : function () {
if (this._tzm) {
this.zone(this._tzm);
} else if (typeof this._i === 'string') {
this.zone(this._i);
}
return this;
},
hasAlignedHourOffset : function (input) {
if (!input) {
input = 0;
}
else {
input = moment(input).zone();
}
return (this.zone() - input) % 60 === 0;
},
daysInMonth : function () {
return daysInMonth(this.year(), this.month());
},
dayOfYear : function (input) {
var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
return input == null ? dayOfYear : this.add("d", (input - dayOfYear));
},
quarter : function () {
return Math.ceil((this.month() + 1.0) / 3.0);
},
weekYear : function (input) {
var year = weekOfYear(this, this.lang()._week.dow, this.lang()._week.doy).year;
return input == null ? year : this.add("y", (input - year));
},
isoWeekYear : function (input) {
var year = weekOfYear(this, 1, 4).year;
return input == null ? year : this.add("y", (input - year));
},
week : function (input) {
var week = this.lang().week(this);
return input == null ? week : this.add("d", (input - week) * 7);
},
isoWeek : function (input) {
var week = weekOfYear(this, 1, 4).week;
return input == null ? week : this.add("d", (input - week) * 7);
},
weekday : function (input) {
var weekday = (this.day() + 7 - this.lang()._week.dow) % 7;
return input == null ? weekday : this.add("d", input - weekday);
},
isoWeekday : function (input) {
// behaves the same as moment#day except
// as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
// as a setter, sunday should belong to the previous week.
return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7);
},
get : function (units) {
units = normalizeUnits(units);
return this[units]();
},
set : function (units, value) {
units = normalizeUnits(units);
if (typeof this[units] === 'function') {
this[units](value);
}
return this;
},
// If passed a language key, it will set the language for this
// instance. Otherwise, it will return the language configuration
// variables for this instance.
lang : function (key) {
if (key === undefined) {
return this._lang;
} else {
this._lang = getLangDefinition(key);
return this;
}
}
});
// helper for adding shortcuts
function makeGetterAndSetter(name, key) {
moment.fn[name] = moment.fn[name + 's'] = function (input) {
var utc = this._isUTC ? 'UTC' : '';
if (input != null) {
this._d['set' + utc + key](input);
moment.updateOffset(this);
return this;
} else {
return this._d['get' + utc + key]();
}
};
}
// loop through and add shortcuts (Month, Date, Hours, Minutes, Seconds, Milliseconds)
for (i = 0; i < proxyGettersAndSetters.length; i ++) {
makeGetterAndSetter(proxyGettersAndSetters[i].toLowerCase().replace(/s$/, ''), proxyGettersAndSetters[i]);
}
// add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear')
makeGetterAndSetter('year', 'FullYear');
// add plural methods
moment.fn.days = moment.fn.day;
moment.fn.months = moment.fn.month;
moment.fn.weeks = moment.fn.week;
moment.fn.isoWeeks = moment.fn.isoWeek;
// add aliased format methods
moment.fn.toJSON = moment.fn.toISOString;
/************************************
Duration Prototype
************************************/
extend(moment.duration.fn = Duration.prototype, {
_bubble : function () {
var milliseconds = this._milliseconds,
days = this._days,
months = this._months,
data = this._data,
seconds, minutes, hours, years;
// The following code bubbles up values, see the tests for
// examples of what that means.
data.milliseconds = milliseconds % 1000;
seconds = absRound(milliseconds / 1000);
data.seconds = seconds % 60;
minutes = absRound(seconds / 60);
data.minutes = minutes % 60;
hours = absRound(minutes / 60);
data.hours = hours % 24;
days += absRound(hours / 24);
data.days = days % 30;
months += absRound(days / 30);
data.months = months % 12;
years = absRound(months / 12);
data.years = years;
},
weeks : function () {
return absRound(this.days() / 7);
},
valueOf : function () {
return this._milliseconds +
this._days * 864e5 +
(this._months % 12) * 2592e6 +
toInt(this._months / 12) * 31536e6;
},
humanize : function (withSuffix) {
var difference = +this,
output = relativeTime(difference, !withSuffix, this.lang());
if (withSuffix) {
output = this.lang().pastFuture(difference, output);
}
return this.lang().postformat(output);
},
add : function (input, val) {
// supports only 2.0-style add(1, 's') or add(moment)
var dur = moment.duration(input, val);
this._milliseconds += dur._milliseconds;
this._days += dur._days;
this._months += dur._months;
this._bubble();
return this;
},
subtract : function (input, val) {
var dur = moment.duration(input, val);
this._milliseconds -= dur._milliseconds;
this._days -= dur._days;
this._months -= dur._months;
this._bubble();
return this;
},
get : function (units) {
units = normalizeUnits(units);
return this[units.toLowerCase() + 's']();
},
as : function (units) {
units = normalizeUnits(units);
return this['as' + units.charAt(0).toUpperCase() + units.slice(1) + 's']();
},
lang : moment.fn.lang,
toIsoString : function () {
// inspired by https://github.com/dordille/moment-isoduration/blob/master/moment.isoduration.js
var years = Math.abs(this.years()),
months = Math.abs(this.months()),
days = Math.abs(this.days()),
hours = Math.abs(this.hours()),
minutes = Math.abs(this.minutes()),
seconds = Math.abs(this.seconds() + this.milliseconds() / 1000);
if (!this.asSeconds()) {
// this is the same as C#'s (Noda) and python (isodate)...
// but not other JS (goog.date)
return 'P0D';
}
return (this.asSeconds() < 0 ? '-' : '') +
'P' +
(years ? years + 'Y' : '') +
(months ? months + 'M' : '') +
(days ? days + 'D' : '') +
((hours || minutes || seconds) ? 'T' : '') +
(hours ? hours + 'H' : '') +
(minutes ? minutes + 'M' : '') +
(seconds ? seconds + 'S' : '');
}
});
function makeDurationGetter(name) {
moment.duration.fn[name] = function () {
return this._data[name];
};
}
function makeDurationAsGetter(name, factor) {
moment.duration.fn['as' + name] = function () {
return +this / factor;
};
}
for (i in unitMillisecondFactors) {
if (unitMillisecondFactors.hasOwnProperty(i)) {
makeDurationAsGetter(i, unitMillisecondFactors[i]);
makeDurationGetter(i.toLowerCase());
}
}
makeDurationAsGetter('Weeks', 6048e5);
moment.duration.fn.asMonths = function () {
return (+this - this.years() * 31536e6) / 2592e6 + this.years() * 12;
};
/************************************
Default Lang
************************************/
// Set default language, other languages will inherit from English.
moment.lang('en', {
ordinal : function (number) {
var b = number % 10,
output = (toInt(number % 100 / 10) === 1) ? 'th' :
(b === 1) ? 'st' :
(b === 2) ? 'nd' :
(b === 3) ? 'rd' : 'th';
return number + output;
}
});
/* EMBED_LANGUAGES */
/************************************
Exposing Moment
************************************/
function makeGlobal(deprecate) {
var warned = false, local_moment = moment;
/*global ender:false */
if (typeof ender !== 'undefined') {
return;
}
// here, `this` means `window` in the browser, or `global` on the server
// add `moment` as a global object via a string identifier,
// for Closure Compiler "advanced" mode
if (deprecate) {
global.moment = function () {
if (!warned && console && console.warn) {
warned = true;
console.warn(
"Accessing Moment through the global scope is " +
"deprecated, and will be removed in an upcoming " +
"release.");
}
return local_moment.apply(null, arguments);
};
extend(global.moment, local_moment);
} else {
global['moment'] = moment;
}
}
// CommonJS module is defined
if (hasModule) {
module.exports = moment;
makeGlobal(true);
} else if (typeof define === "function" && define.amd) {
define("moment", function (require, exports, module) {
if (module.config && module.config() && module.config().noGlobal !== true) {
// If user provided noGlobal, he is aware of global
makeGlobal(module.config().noGlobal === undefined);
}
return moment;
});
} else {
makeGlobal();
}
}).call(this);
//! moment.js
//! version : 2.5.0
//! authors : Tim Wood, Iskren Chernev, Moment.js contributors
//! license : MIT
//! momentjs.com
(function(a){function b(a,b){return function(c){return i(a.call(this,c),b)}}function c(a,b){return function(c){return this.lang().ordinal(a.call(this,c),b)}}function d(){}function e(a){u(a),g(this,a)}function f(a){var b=o(a),c=b.year||0,d=b.month||0,e=b.week||0,f=b.day||0,g=b.hour||0,h=b.minute||0,i=b.second||0,j=b.millisecond||0;this._milliseconds=+j+1e3*i+6e4*h+36e5*g,this._days=+f+7*e,this._months=+d+12*c,this._data={},this._bubble()}function g(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return b.hasOwnProperty("toString")&&(a.toString=b.toString),b.hasOwnProperty("valueOf")&&(a.valueOf=b.valueOf),a}function h(a){return 0>a?Math.ceil(a):Math.floor(a)}function i(a,b,c){for(var d=Math.abs(a)+"",e=a>=0;d.length<b;)d="0"+d;return(e?c?"+":"":"-")+d}function j(a,b,c,d){var e,f,g=b._milliseconds,h=b._days,i=b._months;g&&a._d.setTime(+a._d+g*c),(h||i)&&(e=a.minute(),f=a.hour()),h&&a.date(a.date()+h*c),i&&a.month(a.month()+i*c),g&&!d&&cb.updateOffset(a),(h||i)&&(a.minute(e),a.hour(f))}function k(a){return"[object Array]"===Object.prototype.toString.call(a)}function l(a){return"[object Date]"===Object.prototype.toString.call(a)||a instanceof Date}function m(a,b,c){var d,e=Math.min(a.length,b.length),f=Math.abs(a.length-b.length),g=0;for(d=0;e>d;d++)(c&&a[d]!==b[d]||!c&&q(a[d])!==q(b[d]))&&g++;return g+f}function n(a){if(a){var b=a.toLowerCase().replace(/(.)s$/,"$1");a=Qb[a]||Rb[b]||b}return a}function o(a){var b,c,d={};for(c in a)a.hasOwnProperty(c)&&(b=n(c),b&&(d[b]=a[c]));return d}function p(b){var c,d;if(0===b.indexOf("week"))c=7,d="day";else{if(0!==b.indexOf("month"))return;c=12,d="month"}cb[b]=function(e,f){var g,h,i=cb.fn._lang[b],j=[];if("number"==typeof e&&(f=e,e=a),h=function(a){var b=cb().utc().set(d,a);return i.call(cb.fn._lang,b,e||"")},null!=f)return h(f);for(g=0;c>g;g++)j.push(h(g));return j}}function q(a){var b=+a,c=0;return 0!==b&&isFinite(b)&&(c=b>=0?Math.floor(b):Math.ceil(b)),c}function r(a,b){return new Date(Date.UTC(a,b+1,0)).getUTCDate()}function s(a){return t(a)?366:365}function t(a){return a%4===0&&a%100!==0||a%400===0}function u(a){var b;a._a&&-2===a._pf.overflow&&(b=a._a[ib]<0||a._a[ib]>11?ib:a._a[jb]<1||a._a[jb]>r(a._a[hb],a._a[ib])?jb:a._a[kb]<0||a._a[kb]>23?kb:a._a[lb]<0||a._a[lb]>59?lb:a._a[mb]<0||a._a[mb]>59?mb:a._a[nb]<0||a._a[nb]>999?nb:-1,a._pf._overflowDayOfYear&&(hb>b||b>jb)&&(b=jb),a._pf.overflow=b)}function v(a){a._pf={empty:!1,unusedTokens:[],unusedInput:[],overflow:-2,charsLeftOver:0,nullInput:!1,invalidMonth:null,invalidFormat:!1,userInvalidated:!1,iso:!1}}function w(a){return null==a._isValid&&(a._isValid=!isNaN(a._d.getTime())&&a._pf.overflow<0&&!a._pf.empty&&!a._pf.invalidMonth&&!a._pf.nullInput&&!a._pf.invalidFormat&&!a._pf.userInvalidated,a._strict&&(a._isValid=a._isValid&&0===a._pf.charsLeftOver&&0===a._pf.unusedTokens.length)),a._isValid}function x(a){return a?a.toLowerCase().replace("_","-"):a}function y(a,b){return b._isUTC?cb(a).zone(b._offset||0):cb(a).local()}function z(a,b){return b.abbr=a,ob[a]||(ob[a]=new d),ob[a].set(b),ob[a]}function A(a){delete ob[a]}function B(a){var b,c,d,e,f=0,g=function(a){if(!ob[a]&&pb)try{require("./lang/"+a)}catch(b){}return ob[a]};if(!a)return cb.fn._lang;if(!k(a)){if(c=g(a))return c;a=[a]}for(;f<a.length;){for(e=x(a[f]).split("-"),b=e.length,d=x(a[f+1]),d=d?d.split("-"):null;b>0;){if(c=g(e.slice(0,b).join("-")))return c;if(d&&d.length>=b&&m(e,d,!0)>=b-1)break;b--}f++}return cb.fn._lang}function C(a){return a.match(/\[[\s\S]/)?a.replace(/^\[|\]$/g,""):a.replace(/\\/g,"")}function D(a){var b,c,d=a.match(tb);for(b=0,c=d.length;c>b;b++)d[b]=Vb[d[b]]?Vb[d[b]]:C(d[b]);return function(e){var f="";for(b=0;c>b;b++)f+=d[b]instanceof Function?d[b].call(e,a):d[b];return f}}function E(a,b){return a.isValid()?(b=F(b,a.lang()),Sb[b]||(Sb[b]=D(b)),Sb[b](a)):a.lang().invalidDate()}function F(a,b){function c(a){return b.longDateFormat(a)||a}var d=5;for(ub.lastIndex=0;d>=0&&ub.test(a);)a=a.replace(ub,c),ub.lastIndex=0,d-=1;return a}function G(a,b){var c,d=b._strict;switch(a){case"DDDD":return Gb;case"YYYY":case"GGGG":case"gggg":return d?Hb:xb;case"YYYYYY":case"YYYYY":case"GGGGG":case"ggggg":return d?Ib:yb;case"S":if(d)return Eb;case"SS":if(d)return Fb;case"SSS":case"DDD":return d?Gb:wb;case"MMM":case"MMMM":case"dd":case"ddd":case"dddd":return Ab;case"a":case"A":return B(b._l)._meridiemParse;case"X":return Db;case"Z":case"ZZ":return Bb;case"T":return Cb;case"SSSS":return zb;case"MM":case"DD":case"YY":case"GG":case"gg":case"HH":case"hh":case"mm":case"ss":case"ww":case"WW":return d?Fb:vb;case"M":case"D":case"d":case"H":case"h":case"m":case"s":case"w":case"W":case"e":case"E":return d?Eb:vb;default:return c=new RegExp(O(N(a.replace("\\","")),"i"))}}function H(a){a=a||"";var b=a.match(Bb)||[],c=b[b.length-1]||[],d=(c+"").match(Nb)||["-",0,0],e=+(60*d[1])+q(d[2]);return"+"===d[0]?-e:e}function I(a,b,c){var d,e=c._a;switch(a){case"M":case"MM":null!=b&&(e[ib]=q(b)-1);break;case"MMM":case"MMMM":d=B(c._l).monthsParse(b),null!=d?e[ib]=d:c._pf.invalidMonth=b;break;case"D":case"DD":null!=b&&(e[jb]=q(b));break;case"DDD":case"DDDD":null!=b&&(c._dayOfYear=q(b));break;case"YY":e[hb]=q(b)+(q(b)>68?1900:2e3);break;case"YYYY":case"YYYYY":case"YYYYYY":e[hb]=q(b);break;case"a":case"A":c._isPm=B(c._l).isPM(b);break;case"H":case"HH":case"h":case"hh":e[kb]=q(b);break;case"m":case"mm":e[lb]=q(b);break;case"s":case"ss":e[mb]=q(b);break;case"S":case"SS":case"SSS":case"SSSS":e[nb]=q(1e3*("0."+b));break;case"X":c._d=new Date(1e3*parseFloat(b));break;case"Z":case"ZZ":c._useUTC=!0,c._tzm=H(b);break;case"w":case"ww":case"W":case"WW":case"d":case"dd":case"ddd":case"dddd":case"e":case"E":a=a.substr(0,1);case"gg":case"gggg":case"GG":case"GGGG":case"GGGGG":a=a.substr(0,2),b&&(c._w=c._w||{},c._w[a]=b)}}function J(a){var b,c,d,e,f,g,h,i,j,k,l=[];if(!a._d){for(d=L(a),a._w&&null==a._a[jb]&&null==a._a[ib]&&(f=function(b){var c=parseInt(b,10);return b?b.length<3?c>68?1900+c:2e3+c:c:null==a._a[hb]?cb().weekYear():a._a[hb]},g=a._w,null!=g.GG||null!=g.W||null!=g.E?h=Y(f(g.GG),g.W||1,g.E,4,1):(i=B(a._l),j=null!=g.d?U(g.d,i):null!=g.e?parseInt(g.e,10)+i._week.dow:0,k=parseInt(g.w,10)||1,null!=g.d&&j<i._week.dow&&k++,h=Y(f(g.gg),k,j,i._week.doy,i._week.dow)),a._a[hb]=h.year,a._dayOfYear=h.dayOfYear),a._dayOfYear&&(e=null==a._a[hb]?d[hb]:a._a[hb],a._dayOfYear>s(e)&&(a._pf._overflowDayOfYear=!0),c=T(e,0,a._dayOfYear),a._a[ib]=c.getUTCMonth(),a._a[jb]=c.getUTCDate()),b=0;3>b&&null==a._a[b];++b)a._a[b]=l[b]=d[b];for(;7>b;b++)a._a[b]=l[b]=null==a._a[b]?2===b?1:0:a._a[b];l[kb]+=q((a._tzm||0)/60),l[lb]+=q((a._tzm||0)%60),a._d=(a._useUTC?T:S).apply(null,l)}}function K(a){var b;a._d||(b=o(a._i),a._a=[b.year,b.month,b.day,b.hour,b.minute,b.second,b.millisecond],J(a))}function L(a){var b=new Date;return a._useUTC?[b.getUTCFullYear(),b.getUTCMonth(),b.getUTCDate()]:[b.getFullYear(),b.getMonth(),b.getDate()]}function M(a){a._a=[],a._pf.empty=!0;var b,c,d,e,f,g=B(a._l),h=""+a._i,i=h.length,j=0;for(d=F(a._f,g).match(tb)||[],b=0;b<d.length;b++)e=d[b],c=(h.match(G(e,a))||[])[0],c&&(f=h.substr(0,h.indexOf(c)),f.length>0&&a._pf.unusedInput.push(f),h=h.slice(h.indexOf(c)+c.length),j+=c.length),Vb[e]?(c?a._pf.empty=!1:a._pf.unusedTokens.push(e),I(e,c,a)):a._strict&&!c&&a._pf.unusedTokens.push(e);a._pf.charsLeftOver=i-j,h.length>0&&a._pf.unusedInput.push(h),a._isPm&&a._a[kb]<12&&(a._a[kb]+=12),a._isPm===!1&&12===a._a[kb]&&(a._a[kb]=0),J(a),u(a)}function N(a){return a.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g,function(a,b,c,d,e){return b||c||d||e})}function O(a){return a.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")}function P(a){var b,c,d,e,f;if(0===a._f.length)return a._pf.invalidFormat=!0,a._d=new Date(0/0),void 0;for(e=0;e<a._f.length;e++)f=0,b=g({},a),v(b),b._f=a._f[e],M(b),w(b)&&(f+=b._pf.charsLeftOver,f+=10*b._pf.unusedTokens.length,b._pf.score=f,(null==d||d>f)&&(d=f,c=b));g(a,c||b)}function Q(a){var b,c=a._i,d=Jb.exec(c);if(d){for(a._pf.iso=!0,b=4;b>0;b--)if(d[b]){a._f=Lb[b-1]+(d[6]||" ");break}for(b=0;4>b;b++)if(Mb[b][1].exec(c)){a._f+=Mb[b][0];break}c.match(Bb)&&(a._f+="Z"),M(a)}else a._d=new Date(c)}function R(b){var c=b._i,d=qb.exec(c);c===a?b._d=new Date:d?b._d=new Date(+d[1]):"string"==typeof c?Q(b):k(c)?(b._a=c.slice(0),J(b)):l(c)?b._d=new Date(+c):"object"==typeof c?K(b):b._d=new Date(c)}function S(a,b,c,d,e,f,g){var h=new Date(a,b,c,d,e,f,g);return 1970>a&&h.setFullYear(a),h}function T(a){var b=new Date(Date.UTC.apply(null,arguments));return 1970>a&&b.setUTCFullYear(a),b}function U(a,b){if("string"==typeof a)if(isNaN(a)){if(a=b.weekdaysParse(a),"number"!=typeof a)return null}else a=parseInt(a,10);return a}function V(a,b,c,d,e){return e.relativeTime(b||1,!!c,a,d)}function W(a,b,c){var d=gb(Math.abs(a)/1e3),e=gb(d/60),f=gb(e/60),g=gb(f/24),h=gb(g/365),i=45>d&&["s",d]||1===e&&["m"]||45>e&&["mm",e]||1===f&&["h"]||22>f&&["hh",f]||1===g&&["d"]||25>=g&&["dd",g]||45>=g&&["M"]||345>g&&["MM",gb(g/30)]||1===h&&["y"]||["yy",h];return i[2]=b,i[3]=a>0,i[4]=c,V.apply({},i)}function X(a,b,c){var d,e=c-b,f=c-a.day();return f>e&&(f-=7),e-7>f&&(f+=7),d=cb(a).add("d",f),{week:Math.ceil(d.dayOfYear()/7),year:d.year()}}function Y(a,b,c,d,e){var f,g,h=new Date(i(a,6,!0)+"-01-01").getUTCDay();return c=null!=c?c:e,f=e-h+(h>d?7:0),g=7*(b-1)+(c-e)+f+1,{year:g>0?a:a-1,dayOfYear:g>0?g:s(a-1)+g}}function Z(a){var b=a._i,c=a._f;return"undefined"==typeof a._pf&&v(a),null===b?cb.invalid({nullInput:!0}):("string"==typeof b&&(a._i=b=B().preparse(b)),cb.isMoment(b)?(a=g({},b),a._d=new Date(+b._d)):c?k(c)?P(a):M(a):R(a),new e(a))}function $(a,b){cb.fn[a]=cb.fn[a+"s"]=function(a){var c=this._isUTC?"UTC":"";return null!=a?(this._d["set"+c+b](a),cb.updateOffset(this),this):this._d["get"+c+b]()}}function _(a){cb.duration.fn[a]=function(){return this._data[a]}}function ab(a,b){cb.duration.fn["as"+a]=function(){return+this/b}}function bb(a){var b=!1,c=cb;"undefined"==typeof ender&&(a?(fb.moment=function(){return!b&&console&&console.warn&&(b=!0,console.warn("Accessing Moment through the global scope is deprecated, and will be removed in an upcoming release.")),c.apply(null,arguments)},g(fb.moment,c)):fb.moment=cb)}for(var cb,db,eb="2.5.0",fb=this,gb=Math.round,hb=0,ib=1,jb=2,kb=3,lb=4,mb=5,nb=6,ob={},pb="undefined"!=typeof module&&module.exports&&"undefined"!=typeof require,qb=/^\/?Date\((\-?\d+)/i,rb=/(\-)?(?:(\d*)\.)?(\d+)\:(\d+)(?:\:(\d+)\.?(\d{3})?)?/,sb=/^(-)?P(?:(?:([0-9,.]*)Y)?(?:([0-9,.]*)M)?(?:([0-9,.]*)D)?(?:T(?:([0-9,.]*)H)?(?:([0-9,.]*)M)?(?:([0-9,.]*)S)?)?|([0-9,.]*)W)$/,tb=/(\[[^\[]*\])|(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,4}|X|zz?|ZZ?|.)/g,ub=/(\[[^\[]*\])|(\\)?(LT|LL?L?L?|l{1,4})/g,vb=/\d\d?/,wb=/\d{1,3}/,xb=/\d{1,4}/,yb=/[+\-]?\d{1,6}/,zb=/\d+/,Ab=/[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i,Bb=/Z|[\+\-]\d\d:?\d\d/gi,Cb=/T/i,Db=/[\+\-]?\d+(\.\d{1,3})?/,Eb=/\d/,Fb=/\d\d/,Gb=/\d{3}/,Hb=/\d{4}/,Ib=/[+\-]?\d{6}/,Jb=/^\s*\d{4}-(?:(\d\d-\d\d)|(W\d\d$)|(W\d\d-\d)|(\d\d\d))((T| )(\d\d(:\d\d(:\d\d(\.\d+)?)?)?)?([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?$/,Kb="YYYY-MM-DDTHH:mm:ssZ",Lb=["YYYY-MM-DD","GGGG-[W]WW","GGGG-[W]WW-E","YYYY-DDD"],Mb=[["HH:mm:ss.SSSS",/(T| )\d\d:\d\d:\d\d\.\d{1,3}/],["HH:mm:ss",/(T| )\d\d:\d\d:\d\d/],["HH:mm",/(T| )\d\d:\d\d/],["HH",/(T| )\d\d/]],Nb=/([\+\-]|\d\d)/gi,Ob="Date|Hours|Minutes|Seconds|Milliseconds".split("|"),Pb={Milliseconds:1,Seconds:1e3,Minutes:6e4,Hours:36e5,Days:864e5,Months:2592e6,Years:31536e6},Qb={ms:"millisecond",s:"second",m:"minute",h:"hour",d:"day",D:"date",w:"week",W:"isoWeek",M:"month",y:"year",DDD:"dayOfYear",e:"weekday",E:"isoWeekday",gg:"weekYear",GG:"isoWeekYear"},Rb={dayofyear:"dayOfYear",isoweekday:"isoWeekday",isoweek:"isoWeek",weekyear:"weekYear",isoweekyear:"isoWeekYear"},Sb={},Tb="DDD w W M D d".split(" "),Ub="M D H h m s w W".split(" "),Vb={M:function(){return this.month()+1},MMM:function(a){return this.lang().monthsShort(this,a)},MMMM:function(a){return this.lang().months(this,a)},D:function(){return this.date()},DDD:function(){return this.dayOfYear()},d:function(){return this.day()},dd:function(a){return this.lang().weekdaysMin(this,a)},ddd:function(a){return this.lang().weekdaysShort(this,a)},dddd:function(a){return this.lang().weekdays(this,a)},w:function(){return this.week()},W:function(){return this.isoWeek()},YY:function(){return i(this.year()%100,2)},YYYY:function(){return i(this.year(),4)},YYYYY:function(){return i(this.year(),5)},YYYYYY:function(){var a=this.year(),b=a>=0?"+":"-";return b+i(Math.abs(a),6)},gg:function(){return i(this.weekYear()%100,2)},gggg:function(){return this.weekYear()},ggggg:function(){return i(this.weekYear(),5)},GG:function(){return i(this.isoWeekYear()%100,2)},GGGG:function(){return this.isoWeekYear()},GGGGG:function(){return i(this.isoWeekYear(),5)},e:function(){return this.weekday()},E:function(){return this.isoWeekday()},a:function(){return this.lang().meridiem(this.hours(),this.minutes(),!0)},A:function(){return this.lang().meridiem(this.hours(),this.minutes(),!1)},H:function(){return this.hours()},h:function(){return this.hours()%12||12},m:function(){return this.minutes()},s:function(){return this.seconds()},S:function(){return q(this.milliseconds()/100)},SS:function(){return i(q(this.milliseconds()/10),2)},SSS:function(){return i(this.milliseconds(),3)},SSSS:function(){return i(this.milliseconds(),3)},Z:function(){var a=-this.zone(),b="+";return 0>a&&(a=-a,b="-"),b+i(q(a/60),2)+":"+i(q(a)%60,2)},ZZ:function(){var a=-this.zone(),b="+";return 0>a&&(a=-a,b="-"),b+i(q(a/60),2)+i(q(a)%60,2)},z:function(){return this.zoneAbbr()},zz:function(){return this.zoneName()},X:function(){return this.unix()},Q:function(){return this.quarter()}},Wb=["months","monthsShort","weekdays","weekdaysShort","weekdaysMin"];Tb.length;)db=Tb.pop(),Vb[db+"o"]=c(Vb[db],db);for(;Ub.length;)db=Ub.pop(),Vb[db+db]=b(Vb[db],2);for(Vb.DDDD=b(Vb.DDD,3),g(d.prototype,{set:function(a){var b,c;for(c in a)b=a[c],"function"==typeof b?this[c]=b:this["_"+c]=b},_months:"January_February_March_April_May_June_July_August_September_October_November_December".split("_"),months:function(a){return this._months[a.month()]},_monthsShort:"Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec".split("_"),monthsShort:function(a){return this._monthsShort[a.month()]},monthsParse:function(a){var b,c,d;for(this._monthsParse||(this._monthsParse=[]),b=0;12>b;b++)if(this._monthsParse[b]||(c=cb.utc([2e3,b]),d="^"+this.months(c,"")+"|^"+this.monthsShort(c,""),this._monthsParse[b]=new RegExp(d.replace(".",""),"i")),this._monthsParse[b].test(a))return b},_weekdays:"Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday".split("_"),weekdays:function(a){return this._weekdays[a.day()]},_weekdaysShort:"Sun_Mon_Tue_Wed_Thu_Fri_Sat".split("_"),weekdaysShort:function(a){return this._weekdaysShort[a.day()]},_weekdaysMin:"Su_Mo_Tu_We_Th_Fr_Sa".split("_"),weekdaysMin:function(a){return this._weekdaysMin[a.day()]},weekdaysParse:function(a){var b,c,d;for(this._weekdaysParse||(this._weekdaysParse=[]),b=0;7>b;b++)if(this._weekdaysParse[b]||(c=cb([2e3,1]).day(b),d="^"+this.weekdays(c,"")+"|^"+this.weekdaysShort(c,"")+"|^"+this.weekdaysMin(c,""),this._weekdaysParse[b]=new RegExp(d.replace(".",""),"i")),this._weekdaysParse[b].test(a))return b},_longDateFormat:{LT:"h:mm A",L:"MM/DD/YYYY",LL:"MMMM D YYYY",LLL:"MMMM D YYYY LT",LLLL:"dddd, MMMM D YYYY LT"},longDateFormat:function(a){var b=this._longDateFormat[a];return!b&&this._longDateFormat[a.toUpperCase()]&&(b=this._longDateFormat[a.toUpperCase()].replace(/MMMM|MM|DD|dddd/g,function(a){return a.slice(1)}),this._longDateFormat[a]=b),b},isPM:function(a){return"p"===(a+"").toLowerCase().charAt(0)},_meridiemParse:/[ap]\.?m?\.?/i,meridiem:function(a,b,c){return a>11?c?"pm":"PM":c?"am":"AM"},_calendar:{sameDay:"[Today at] LT",nextDay:"[Tomorrow at] LT",nextWeek:"dddd [at] LT",lastDay:"[Yesterday at] LT",lastWeek:"[Last] dddd [at] LT",sameElse:"L"},calendar:function(a,b){var c=this._calendar[a];return"function"==typeof c?c.apply(b):c},_relativeTime:{future:"in %s",past:"%s ago",s:"a few seconds",m:"a minute",mm:"%d minutes",h:"an hour",hh:"%d hours",d:"a day",dd:"%d days",M:"a month",MM:"%d months",y:"a year",yy:"%d years"},relativeTime:function(a,b,c,d){var e=this._relativeTime[c];return"function"==typeof e?e(a,b,c,d):e.replace(/%d/i,a)},pastFuture:function(a,b){var c=this._relativeTime[a>0?"future":"past"];return"function"==typeof c?c(b):c.replace(/%s/i,b)},ordinal:function(a){return this._ordinal.replace("%d",a)},_ordinal:"%d",preparse:function(a){return a},postformat:function(a){return a},week:function(a){return X(a,this._week.dow,this._week.doy).week},_week:{dow:0,doy:6},_invalidDate:"Invalid date",invalidDate:function(){return this._invalidDate}}),cb=function(b,c,d,e){return"boolean"==typeof d&&(e=d,d=a),Z({_i:b,_f:c,_l:d,_strict:e,_isUTC:!1})},cb.utc=function(b,c,d,e){var f;return"boolean"==typeof d&&(e=d,d=a),f=Z({_useUTC:!0,_isUTC:!0,_l:d,_i:b,_f:c,_strict:e}).utc()},cb.unix=function(a){return cb(1e3*a)},cb.duration=function(a,b){var c,d,e,g=a,h=null;return cb.isDuration(a)?g={ms:a._milliseconds,d:a._days,M:a._months}:"number"==typeof a?(g={},b?g[b]=a:g.milliseconds=a):(h=rb.exec(a))?(c="-"===h[1]?-1:1,g={y:0,d:q(h[jb])*c,h:q(h[kb])*c,m:q(h[lb])*c,s:q(h[mb])*c,ms:q(h[nb])*c}):(h=sb.exec(a))&&(c="-"===h[1]?-1:1,e=function(a){var b=a&&parseFloat(a.replace(",","."));return(isNaN(b)?0:b)*c},g={y:e(h[2]),M:e(h[3]),d:e(h[4]),h:e(h[5]),m:e(h[6]),s:e(h[7]),w:e(h[8])}),d=new f(g),cb.isDuration(a)&&a.hasOwnProperty("_lang")&&(d._lang=a._lang),d},cb.version=eb,cb.defaultFormat=Kb,cb.updateOffset=function(){},cb.lang=function(a,b){var c;return a?(b?z(x(a),b):null===b?(A(a),a="en"):ob[a]||B(a),c=cb.duration.fn._lang=cb.fn._lang=B(a),c._abbr):cb.fn._lang._abbr},cb.langData=function(a){return a&&a._lang&&a._lang._abbr&&(a=a._lang._abbr),B(a)},cb.isMoment=function(a){return a instanceof e},cb.isDuration=function(a){return a instanceof f},db=Wb.length-1;db>=0;--db)p(Wb[db]);for(cb.normalizeUnits=function(a){return n(a)},cb.invalid=function(a){var b=cb.utc(0/0);return null!=a?g(b._pf,a):b._pf.userInvalidated=!0,b},cb.parseZone=function(a){return cb(a).parseZone()},g(cb.fn=e.prototype,{clone:function(){return cb(this)},valueOf:function(){return+this._d+6e4*(this._offset||0)},unix:function(){return Math.floor(+this/1e3)},toString:function(){return this.clone().lang("en").format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ")},toDate:function(){return this._offset?new Date(+this):this._d},toISOString:function(){var a=cb(this).utc();return 0<a.year()&&a.year()<=9999?E(a,"YYYY-MM-DD[T]HH:mm:ss.SSS[Z]"):E(a,"YYYYYY-MM-DD[T]HH:mm:ss.SSS[Z]")},toArray:function(){var a=this;return[a.year(),a.month(),a.date(),a.hours(),a.minutes(),a.seconds(),a.milliseconds()]},isValid:function(){return w(this)},isDSTShifted:function(){return this._a?this.isValid()&&m(this._a,(this._isUTC?cb.utc(this._a):cb(this._a)).toArray())>0:!1},parsingFlags:function(){return g({},this._pf)},invalidAt:function(){return this._pf.overflow},utc:function(){return this.zone(0)},local:function(){return this.zone(0),this._isUTC=!1,this},format:function(a){var b=E(this,a||cb.defaultFormat);return this.lang().postformat(b)},add:function(a,b){var c;return c="string"==typeof a?cb.duration(+b,a):cb.duration(a,b),j(this,c,1),this},subtract:function(a,b){var c;return c="string"==typeof a?cb.duration(+b,a):cb.duration(a,b),j(this,c,-1),this},diff:function(a,b,c){var d,e,f=y(a,this),g=6e4*(this.zone()-f.zone());return b=n(b),"year"===b||"month"===b?(d=432e5*(this.daysInMonth()+f.daysInMonth()),e=12*(this.year()-f.year())+(this.month()-f.month()),e+=(this-cb(this).startOf("month")-(f-cb(f).startOf("month")))/d,e-=6e4*(this.zone()-cb(this).startOf("month").zone()-(f.zone()-cb(f).startOf("month").zone()))/d,"year"===b&&(e/=12)):(d=this-f,e="second"===b?d/1e3:"minute"===b?d/6e4:"hour"===b?d/36e5:"day"===b?(d-g)/864e5:"week"===b?(d-g)/6048e5:d),c?e:h(e)},from:function(a,b){return cb.duration(this.diff(a)).lang(this.lang()._abbr).humanize(!b)},fromNow:function(a){return this.from(cb(),a)},calendar:function(){var a=y(cb(),this).startOf("day"),b=this.diff(a,"days",!0),c=-6>b?"sameElse":-1>b?"lastWeek":0>b?"lastDay":1>b?"sameDay":2>b?"nextDay":7>b?"nextWeek":"sameElse";return this.format(this.lang().calendar(c,this))},isLeapYear:function(){return t(this.year())},isDST:function(){return this.zone()<this.clone().month(0).zone()||this.zone()<this.clone().month(5).zone()},day:function(a){var b=this._isUTC?this._d.getUTCDay():this._d.getDay();return null!=a?(a=U(a,this.lang()),this.add({d:a-b})):b},month:function(a){var b,c=this._isUTC?"UTC":"";return null!=a?"string"==typeof a&&(a=this.lang().monthsParse(a),"number"!=typeof a)?this:(b=this.date(),this.date(1),this._d["set"+c+"Month"](a),this.date(Math.min(b,this.daysInMonth())),cb.updateOffset(this),this):this._d["get"+c+"Month"]()},startOf:function(a){switch(a=n(a)){case"year":this.month(0);case"month":this.date(1);case"week":case"isoWeek":case"day":this.hours(0);case"hour":this.minutes(0);case"minute":this.seconds(0);case"second":this.milliseconds(0)}return"week"===a?this.weekday(0):"isoWeek"===a&&this.isoWeekday(1),this},endOf:function(a){return a=n(a),this.startOf(a).add("isoWeek"===a?"week":a,1).subtract("ms",1)},isAfter:function(a,b){return b="undefined"!=typeof b?b:"millisecond",+this.clone().startOf(b)>+cb(a).startOf(b)},isBefore:function(a,b){return b="undefined"!=typeof b?b:"millisecond",+this.clone().startOf(b)<+cb(a).startOf(b)},isSame:function(a,b){return b=b||"ms",+this.clone().startOf(b)===+y(a,this).startOf(b)},min:function(a){return a=cb.apply(null,arguments),this>a?this:a},max:function(a){return a=cb.apply(null,arguments),a>this?this:a},zone:function(a){var b=this._offset||0;return null==a?this._isUTC?b:this._d.getTimezoneOffset():("string"==typeof a&&(a=H(a)),Math.abs(a)<16&&(a=60*a),this._offset=a,this._isUTC=!0,b!==a&&j(this,cb.duration(b-a,"m"),1,!0),this)},zoneAbbr:function(){return this._isUTC?"UTC":""},zoneName:function(){return this._isUTC?"Coordinated Universal Time":""},parseZone:function(){return this._tzm?this.zone(this._tzm):"string"==typeof this._i&&this.zone(this._i),this},hasAlignedHourOffset:function(a){return a=a?cb(a).zone():0,(this.zone()-a)%60===0},daysInMonth:function(){return r(this.year(),this.month())},dayOfYear:function(a){var b=gb((cb(this).startOf("day")-cb(this).startOf("year"))/864e5)+1;return null==a?b:this.add("d",a-b)},quarter:function(){return Math.ceil((this.month()+1)/3)},weekYear:function(a){var b=X(this,this.lang()._week.dow,this.lang()._week.doy).year;return null==a?b:this.add("y",a-b)},isoWeekYear:function(a){var b=X(this,1,4).year;return null==a?b:this.add("y",a-b)},week:function(a){var b=this.lang().week(this);return null==a?b:this.add("d",7*(a-b))},isoWeek:function(a){var b=X(this,1,4).week;return null==a?b:this.add("d",7*(a-b))},weekday:function(a){var b=(this.day()+7-this.lang()._week.dow)%7;return null==a?b:this.add("d",a-b)},isoWeekday:function(a){return null==a?this.day()||7:this.day(this.day()%7?a:a-7)},get:function(a){return a=n(a),this[a]()},set:function(a,b){return a=n(a),"function"==typeof this[a]&&this[a](b),this},lang:function(b){return b===a?this._lang:(this._lang=B(b),this)}}),db=0;db<Ob.length;db++)$(Ob[db].toLowerCase().replace(/s$/,""),Ob[db]);$("year","FullYear"),cb.fn.days=cb.fn.day,cb.fn.months=cb.fn.month,cb.fn.weeks=cb.fn.week,cb.fn.isoWeeks=cb.fn.isoWeek,cb.fn.toJSON=cb.fn.toISOString,g(cb.duration.fn=f.prototype,{_bubble:function(){var a,b,c,d,e=this._milliseconds,f=this._days,g=this._months,i=this._data;i.milliseconds=e%1e3,a=h(e/1e3),i.seconds=a%60,b=h(a/60),i.minutes=b%60,c=h(b/60),i.hours=c%24,f+=h(c/24),i.days=f%30,g+=h(f/30),i.months=g%12,d=h(g/12),i.years=d},weeks:function(){return h(this.days()/7)},valueOf:function(){return this._milliseconds+864e5*this._days+this._months%12*2592e6+31536e6*q(this._months/12)},humanize:function(a){var b=+this,c=W(b,!a,this.lang());return a&&(c=this.lang().pastFuture(b,c)),this.lang().postformat(c)},add:function(a,b){var c=cb.duration(a,b);return this._milliseconds+=c._milliseconds,this._days+=c._days,this._months+=c._months,this._bubble(),this},subtract:function(a,b){var c=cb.duration(a,b);return this._milliseconds-=c._milliseconds,this._days-=c._days,this._months-=c._months,this._bubble(),this},get:function(a){return a=n(a),this[a.toLowerCase()+"s"]()},as:function(a){return a=n(a),this["as"+a.charAt(0).toUpperCase()+a.slice(1)+"s"]()},lang:cb.fn.lang,toIsoString:function(){var a=Math.abs(this.years()),b=Math.abs(this.months()),c=Math.abs(this.days()),d=Math.abs(this.hours()),e=Math.abs(this.minutes()),f=Math.abs(this.seconds()+this.milliseconds()/1e3);return this.asSeconds()?(this.asSeconds()<0?"-":"")+"P"+(a?a+"Y":"")+(b?b+"M":"")+(c?c+"D":"")+(d||e||f?"T":"")+(d?d+"H":"")+(e?e+"M":"")+(f?f+"S":""):"P0D"}});for(db in Pb)Pb.hasOwnProperty(db)&&(ab(db,Pb[db]),_(db.toLowerCase()));ab("Weeks",6048e5),cb.duration.fn.asMonths=function(){return(+this-31536e6*this.years())/2592e6+12*this.years()},cb.lang("en",{ordinal:function(a){var b=a%10,c=1===q(a%100/10)?"th":1===b?"st":2===b?"nd":3===b?"rd":"th";return a+c}}),pb?(module.exports=cb,bb(!0)):"function"==typeof define&&define.amd?define("moment",function(b,c,d){return d.config&&d.config()&&d.config().noGlobal!==!0&&bb(d.config().noGlobal===a),cb}):bb()}).call(this);
\ No newline at end of file
/*jslint indent: 2, nomen: true */
/*global module, define, exports, window, moment */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(exports, moment);
}
window.jiodate = {};
module(window.jiodate, moment);
}(['exports', 'moment'], function (to_export, moment) {
"use strict";
/**
* Add a secured (write permission denied) property to an object.
*
* @param {Object} object The object to fill
* @param {String} key The object key where to store the property
* @param {Any} value The value to store
*/
function _export(key, value) {
Object.defineProperty(to_export, key, {
"configurable": false,
"enumerable": true,
"writable": false,
"value": value
});
}
var YEAR = 'year',
MONTH = 'month',
DAY = 'day',
HOUR = 'hour',
MIN = 'minute',
SEC = 'second',
MSEC = 'millisecond',
precision_grade = {
'year': 0,
'month': 1,
'day': 2,
'hour': 3,
'minute': 4,
'second': 5,
'millisecond': 6
},
lesserPrecision = function (p1, p2) {
return (precision_grade[p1] < precision_grade[p2]) ? p1 : p2;
},
JIODate = null;
JIODate = function (str) {
// in case of forgotten 'new'
if (!(this instanceof JIODate)) {
return new JIODate(str);
}
if (str instanceof JIODate) {
this.mom = str.mom.clone();
this._precision = str._precision;
return;
}
if (str === undefined) {
this.mom = moment();
this.setPrecision(MSEC);
return;
}
this.mom = null;
// http://www.w3.org/TR/NOTE-datetime
// http://dotat.at/tmp/ISO_8601-2004_E.pdf
// XXX these regexps fail to detect many invalid dates.
if (str.match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+\-][0-2]\d:[0-5]\d|Z)/)
|| str.match(/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d/)) {
// ISO, milliseconds
this.mom = moment(str);
this.setPrecision(MSEC);
} else if (str.match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+\-][0-2]\d:[0-5]\d|Z)/)
|| str.match(/\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d/)) {
// ISO, seconds
this.mom = moment(str);
this.setPrecision(SEC);
} else if (str.match(/\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+\-][0-2]\d:[0-5]\d|Z)/)
|| str.match(/\d\d\d\d-\d\d-\d\d \d\d:\d\d/)) {
// ISO, minutes
this.mom = moment(str);
this.setPrecision(MIN);
} else if (str.match(/\d\d\d\d-\d\d-\d\d \d\d/)) {
this.mom = moment(str);
this.setPrecision(HOUR);
} else if (str.match(/\d\d\d\d-\d\d-\d\d/)) {
this.mom = moment(str);
this.setPrecision(DAY);
} else if (str.match(/\d\d\d\d-\d\d/)) {
this.mom = moment(str);
this.setPrecision(MONTH);
} else if (str.match(/\d\d\d\d/)) {
this.mom = moment(str);
this.setPrecision(YEAR);
}
if (!this.mom) {
throw new Error("Cannot parse: " + str);
}
};
JIODate.prototype.setPrecision = function (prec) {
this._precision = prec;
};
JIODate.prototype.getPrecision = function () {
return this._precision;
};
JIODate.prototype.cmp = function (other) {
var m1 = this.mom,
m2 = other.mom,
p = lesserPrecision(this._precision, other._precision);
return m1.isBefore(m2, p) ? -1 : (m1.isSame(m2, p) ? 0 : +1);
};
JIODate.prototype.toPrecisionString = function (precision) {
var fmt;
precision = precision || this._precision;
fmt = {
'millisecond': 'YYYY-MM-DD HH:mm:ss.SSS',
'second': 'YYYY-MM-DD HH:mm:ss',
'minute': 'YYYY-MM-DD HH:mm',
'hour': 'YYYY-MM-DD HH',
'day': 'YYYY-MM-DD',
'month': 'YYYY-MM',
'year': 'YYYY'
}[precision];
if (!fmt) {
throw new TypeError("Unsupported precision value '" + precision + "'");
}
return this.mom.format(fmt);
};
_export('JIODate', JIODate);
_export('YEAR', YEAR);
_export('MONTH', MONTH);
_export('DAY', DAY);
_export('HOUR', HOUR);
_export('MIN', MIN);
_export('SEC', SEC);
_export('MSEC', MSEC);
return to_export;
}));
......@@ -142,6 +142,7 @@
this._database = localStorage;
this._storage = localstorage;
this._mode = "localStorage";
this._key_schema = spec.key_schema;
break;
}
}
......@@ -445,7 +446,8 @@
document_object[meta._id] = meta;
});
}
complex_queries.QueryFactory.create(options.query || "").
complex_queries.QueryFactory.create(options.query || "",
this._key_schema).
exec(document_list, options).then(function () {
document_list = document_list.map(function (value) {
var o = {
......
......@@ -13,7 +13,7 @@
* @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare
*/
function ComplexQuery(spec) {
function ComplexQuery(spec, key_schema) {
Query.call(this);
/**
......@@ -35,7 +35,12 @@ function ComplexQuery(spec) {
* @optional
*/
this.query_list = spec.query_list || [];
this.query_list = this.query_list.map(QueryFactory.create);
/*jslint unparam: true*/
this.query_list = this.query_list.map(
// decorate the map to avoid sending the index as key_schema argument
function (o, i) { return QueryFactory.create(o, key_schema); }
);
/*jslint unparam: false*/
}
inherits(ComplexQuery, Query);
......
......@@ -8,9 +8,9 @@
'AND' AND ;
'OR' OR ;
'NOT' NOT ;
'[^><= :\(\)"][^ :\(\)"]*:' COLUMN ;
'[^><!= :\(\)"][^ :\(\)"]*:' COLUMN ;
'"(\\.|[^\\"])*"' STRING ;
'[^><= :\(\)"][^ :\(\)"]*' WORD ;
'[^><!= :\(\)"][^ :\(\)"]*' WORD ;
'(>=?|<=?|!?=)' OPERATOR ;
##
......
......@@ -21,7 +21,7 @@ function QueryFactory() {
* of a Query
* @return {Query} A Query object
*/
QueryFactory.create = function (object) {
QueryFactory.create = function (object, key_schema) {
if (object === "") {
return new Query();
}
......@@ -30,7 +30,7 @@ QueryFactory.create = function (object) {
}
if (typeof (object || {}).type === "string" &&
query_class_dict[object.type]) {
return new query_class_dict[object.type](object);
return new query_class_dict[object.type](object, key_schema);
}
throw new TypeError("QueryFactory.create(): " +
"Argument 1 is not a search text or a parsable object");
......
......@@ -2,6 +2,36 @@
/*global Query: true, inherits: true, query_class_dict: true, _export: true,
convertStringToRegExp, RSVP */
var checkKeySchema = function (key_schema) {
var prop;
if (key_schema !== undefined) {
if (typeof key_schema !== 'object') {
throw new TypeError("SimpleQuery().create(): " +
"key_schema is not of type 'object'");
}
// key_set is mandatory
if (key_schema.key_set === undefined) {
throw new TypeError("SimpleQuery().create(): " +
"key_schema has no 'key_set' property");
}
for (prop in key_schema) {
if (key_schema.hasOwnProperty(prop)) {
switch (prop) {
case 'key_set':
case 'cast_lookup':
case 'match_lookup':
break;
default:
throw new TypeError("SimpleQuery().create(): " +
"key_schema has unknown property '" + prop + "'");
}
}
}
}
};
/**
* The SimpleQuery inherits from Query, and compares one metadata value
*
......@@ -12,9 +42,13 @@
* @param {String} spec.key The metadata key
* @param {String} spec.value The value of the metadata to compare
*/
function SimpleQuery(spec) {
function SimpleQuery(spec, key_schema) {
Query.call(this);
checkKeySchema(key_schema);
this._key_schema = key_schema || {};
/**
* Operator to use to compare object values
*
......@@ -44,11 +78,84 @@ function SimpleQuery(spec) {
}
inherits(SimpleQuery, Query);
var checkKey = function (key) {
var prop;
if (key.read_from === undefined) {
throw new TypeError("Custom key is missing the read_from property");
}
for (prop in key) {
if (key.hasOwnProperty(prop)) {
switch (prop) {
case 'read_from':
case 'cast_to':
case 'equal_match':
break;
default:
throw new TypeError("Custom key has unknown property '" +
prop + "'");
}
}
}
};
/**
* #crossLink "Query/match:method"
*/
SimpleQuery.prototype.match = function (item, wildcard_character) {
return this[this.operator](item[this.key], this.value, wildcard_character);
var object_value = null,
equal_match = null,
cast_to = null,
matchMethod = null,
value = null,
key = this.key;
matchMethod = this[this.operator];
if (this._key_schema.key_set && this._key_schema.key_set[key] !== undefined) {
key = this._key_schema.key_set[key];
}
if (typeof key === 'object') {
checkKey(key);
object_value = item[key.read_from];
equal_match = key.equal_match;
// equal_match can be a string
if (typeof equal_match === 'string') {
// XXX raise error if equal_match not in match_lookup
equal_match = this._key_schema.match_lookup[equal_match];
}
// equal_match overrides the default '=' operator
if (equal_match !== undefined) {
matchMethod = (this.operator === '=') ? equal_match : matchMethod;
}
value = this.value;
cast_to = key.cast_to;
if (cast_to) {
// cast_to can be a string
if (typeof cast_to === 'string') {
// XXX raise error if cast_to not in cast_lookup
cast_to = this._key_schema.cast_lookup[cast_to];
}
value = cast_to(value);
object_value = cast_to(object_value);
}
} else {
object_value = item[key];
value = this.value;
}
if (object_value === undefined || value === undefined) {
return RSVP.resolve(false);
}
return matchMethod(object_value, value, wildcard_character);
};
/**
......@@ -88,17 +195,12 @@ SimpleQuery.prototype["="] = function (object_value, comparison_value,
}
for (i = 0; i < object_value.length; i += 1) {
value = object_value[i];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (comparison_value === undefined) {
if (value === undefined) {
return RSVP.resolve(true);
}
return RSVP.resolve(false);
}
if (value === undefined) {
return RSVP.resolve(false);
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value,
wildcard_character) === 0);
}
if (
convertStringToRegExp(
......@@ -129,17 +231,12 @@ SimpleQuery.prototype["!="] = function (object_value, comparison_value,
}
for (i = 0; i < object_value.length; i += 1) {
value = object_value[i];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (comparison_value === undefined) {
if (value === undefined) {
return RSVP.resolve(false);
}
return RSVP.resolve(true);
}
if (value === undefined) {
return RSVP.resolve(true);
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value,
wildcard_character) !== 0);
}
if (
convertStringToRegExp(
......@@ -167,9 +264,12 @@ SimpleQuery.prototype["<"] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) < 0);
}
return RSVP.resolve(value < comparison_value);
};
......@@ -188,9 +288,12 @@ SimpleQuery.prototype["<="] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) <= 0);
}
return RSVP.resolve(value <= comparison_value);
};
......@@ -209,9 +312,12 @@ SimpleQuery.prototype[">"] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) > 0);
}
return RSVP.resolve(value > comparison_value);
};
......@@ -230,9 +336,12 @@ SimpleQuery.prototype[">="] = function (object_value, comparison_value) {
object_value = [object_value];
}
value = object_value[0];
if (typeof value === 'object') {
if (typeof value === 'object' && value.hasOwnProperty('content')) {
value = value.content;
}
if (value.cmp !== undefined) {
return RSVP.resolve(value.cmp(comparison_value) >= 0);
}
return RSVP.resolve(value >= comparison_value);
};
......
/*jslint indent: 2, newcap: true */
/*global define, exports, require, test, ok, strictEqual, equal, throws, jiodate, moment, module */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('jiodate'), require('moment'));
}
module(jiodate, moment);
}(['jiodate', 'moment', 'qunit'], function (jiodate, moment) {
"use strict";
module('JIODate');
var JIODate = jiodate.JIODate;
test("A JIODate can be instantiated without parameters (=now)", function () {
ok((new JIODate()) instanceof JIODate);
ok(JIODate() instanceof JIODate);
});
test("Parsing from ISO string and exposing Moment/Date objects", function () {
var d = JIODate('2012-03-04T08:52:13.746Z');
ok(moment.isMoment(d.mom));
strictEqual(d.mom.toISOString(), '2012-03-04T08:52:13.746Z');
strictEqual(d.mom.year(), 2012);
strictEqual(d.mom.month(), 2);
strictEqual(d.mom.date(), 4);
// and so on..
strictEqual(d.mom.isoWeekday(), 7);
strictEqual(d.mom.dayOfYear(), 64);
strictEqual(d.mom.week(), 10);
strictEqual(d.mom.isoWeek(), 9);
strictEqual(d.mom.day(), 0);
strictEqual(d.mom.hours(), 9);
strictEqual(d.mom.minutes(), 52);
strictEqual(d.mom.seconds(), 13);
strictEqual(d.mom.milliseconds(), 746);
// careful: changing the Date object changes the moment as well
ok(d.mom.toDate() instanceof Date);
});
test("By default, maximum precision is kept, but it can be changed later", function () {
var d = new JIODate();
equal(d.getPrecision(), jiodate.MSEC);
d.setPrecision(jiodate.SEC);
equal(d.getPrecision(), jiodate.SEC);
d.setPrecision(jiodate.DAY);
equal(d.getPrecision(), jiodate.DAY);
d.setPrecision(jiodate.MONTH);
equal(d.getPrecision(), jiodate.MONTH);
});
test("Passing a JIODate object to the constructor clones it", function () {
var d = JIODate('2012-05-06');
strictEqual(d.cmp(JIODate(d)), 0);
});
test("Comparison with .cmp() - any precision", function () {
var data = [
[
jiodate.MSEC,
'2012-03-04T08:52:13.746Z',
'2012-03-04T08:52:13.746Z',
'2012-03-04T08:52:13.747Z'
], [
jiodate.SEC,
'2012-03-04T08:52:13.746Z',
'2012-03-04T08:52:13.999Z',
'2012-03-04T08:52:14.746Z'
], [
jiodate.MIN,
'2012-03-04T08:52:13.746Z',
'2012-03-04T08:52:59.999Z',
'2012-03-04T08:53:13.746Z'
], [
jiodate.HOUR,
'2012-03-04T08:52:13.746Z',
'2012-03-04T08:59:59.999Z',
'2012-03-04T09:52:13.746Z'
], [
jiodate.DAY,
'2012-03-04T08:52:13.746Z',
'2012-03-04T20:59:59.999Z',
'2012-03-05T08:52:13.746Z'
], [
jiodate.MONTH,
'2012-03-04T08:52:13.746Z',
'2012-03-31T20:59:59.999Z',
'2012-04-04T08:53:13.746Z'
], [
jiodate.YEAR,
'2012-03-04T08:52:13.746Z',
'2012-12-31T20:59:59.999Z',
'2013-03-04T08:53:13.746Z'
]
], i = 0, precision, d1, d2, d3, s1, s2, s3, mp;
for (i = 0; i < data.length; i += 1) {
precision = data[i][0];
d1 = JIODate(data[i][1]);
d2 = JIODate(data[i][2]);
d3 = JIODate(data[i][3]);
d1.setPrecision(precision);
d2.setPrecision(precision);
d3.setPrecision(precision);
s1 = d1.mom.format();
s2 = d2.mom.format();
s3 = d3.mom.format();
mp = ' - ' + precision;
strictEqual(d1.cmp(d2), 0, s1 + ' cmp ' + s2 + mp);
strictEqual(d1.cmp(d3), -1, s1 + ' cmp ' + s3 + mp);
strictEqual(d2.cmp(d1), 0, s2 + ' cmp ' + s1 + mp);
strictEqual(d2.cmp(d3), -1, s2 + ' cmp ' + s3 + mp);
strictEqual(d3.cmp(d1), 1, s3 + ' cmp ' + s1 + mp);
strictEqual(d3.cmp(d2), 1, s1 + ' cmp ' + s2 + mp);
}
});
test("Display timestamp value trucated to precision", function () {
var d = JIODate('2012-03-04T08:52:13.746Z');
// XXX No timezone
strictEqual(d.toPrecisionString(jiodate.MSEC), '2012-03-04 09:52:13.746');
strictEqual(d.toPrecisionString(jiodate.SEC), '2012-03-04 09:52:13');
strictEqual(d.toPrecisionString(jiodate.MIN), '2012-03-04 09:52');
strictEqual(d.toPrecisionString(jiodate.HOUR), '2012-03-04 09');
strictEqual(d.toPrecisionString(jiodate.DAY), '2012-03-04');
strictEqual(d.toPrecisionString(jiodate.MONTH), '2012-03');
strictEqual(d.toPrecisionString(jiodate.YEAR), '2012');
throws(
function () {
d.toPrecisionString('something');
},
/Unsupported precision value 'something'/,
"Precision parameter must be a valid value"
);
d.setPrecision(jiodate.HOUR);
strictEqual(d.toPrecisionString(), '2012-03-04 09');
});
test("Parsing of invalid input", function () {
throws(
function () {
JIODate('foobar');
},
/Cannot parse: foobar/,
"Invalid strings raise exceptions"
);
});
test("Parsing of partial timestamp values with any precision", function () {
var d;
d = JIODate('2012-05-02 06:07:08.989');
strictEqual(d.getPrecision(), 'millisecond');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08.989');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:08.989Z');
d = JIODate('2012-05-02 06:07:08');
strictEqual(d.getPrecision(), 'second');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07:08');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:08.000Z');
d = JIODate('2012-05-02 06:07');
strictEqual(d.getPrecision(), 'minute');
strictEqual(d.toPrecisionString(), '2012-05-02 06:07');
strictEqual(d.mom.toISOString(), '2012-05-02T04:07:00.000Z');
d = JIODate('2012-05-02 06');
strictEqual(d.getPrecision(), 'hour');
strictEqual(d.toPrecisionString(), '2012-05-02 06');
strictEqual(d.mom.toISOString(), '2012-05-02T04:00:00.000Z');
d = JIODate('2012-05-02');
strictEqual(d.getPrecision(), 'day');
strictEqual(d.toPrecisionString(), '2012-05-02');
strictEqual(d.mom.toISOString(), '2012-05-01T22:00:00.000Z');
d = JIODate('2012-05');
strictEqual(d.getPrecision(), 'month');
strictEqual(d.toPrecisionString(), '2012-05');
strictEqual(d.mom.toISOString(), '2012-05-01T00:00:00.000Z');
d = JIODate('2012');
strictEqual(d.getPrecision(), 'year');
strictEqual(d.toPrecisionString(), '2012');
strictEqual(d.mom.toISOString(), '2012-01-01T00:00:00.000Z');
});
test("Comparison between heterogeneous values is done with the lesser precision", function () {
var dmsec = JIODate('2012-05-02 06:07:08.989'),
dsec = JIODate('2012-05-02 06:07:08'),
dmin = JIODate('2012-05-02 06:07'),
dhour = JIODate('2012-05-02 06'),
dday = JIODate('2012-05-02'),
dmonth = JIODate('2012-05'),
dyear = JIODate('2012');
strictEqual(dmsec.cmp(dsec), 0);
strictEqual(dmsec.cmp(dmin), 0);
strictEqual(dmsec.cmp(dhour), 0);
strictEqual(dmsec.cmp(dday), 0);
strictEqual(dmsec.cmp(dmonth), 0);
strictEqual(dmsec.cmp(dyear), 0);
strictEqual(dsec.cmp(dmsec), 0);
strictEqual(dsec.cmp(dmin), 0);
strictEqual(dsec.cmp(dhour), 0);
strictEqual(dsec.cmp(dday), 0);
strictEqual(dsec.cmp(dmonth), 0);
strictEqual(dsec.cmp(dyear), 0);
strictEqual(dmin.cmp(dmsec), 0);
strictEqual(dmin.cmp(dsec), 0);
strictEqual(dmin.cmp(dhour), 0);
strictEqual(dmin.cmp(dday), 0);
strictEqual(dmin.cmp(dmonth), 0);
strictEqual(dmin.cmp(dyear), 0);
strictEqual(dhour.cmp(dmsec), 0);
strictEqual(dhour.cmp(dsec), 0);
strictEqual(dhour.cmp(dmin), 0);
strictEqual(dhour.cmp(dday), 0);
strictEqual(dhour.cmp(dmonth), 0);
strictEqual(dhour.cmp(dyear), 0);
strictEqual(dday.cmp(dmsec), 0);
strictEqual(dday.cmp(dsec), 0);
strictEqual(dday.cmp(dmin), 0);
strictEqual(dday.cmp(dhour), 0);
strictEqual(dday.cmp(dmonth), 0);
strictEqual(dday.cmp(dyear), 0);
strictEqual(dmonth.cmp(dmsec), 0);
strictEqual(dmonth.cmp(dsec), 0);
strictEqual(dmonth.cmp(dmin), 0);
strictEqual(dmonth.cmp(dhour), 0);
strictEqual(dmonth.cmp(dday), 0);
strictEqual(dmonth.cmp(dyear), 0);
strictEqual(dyear.cmp(dmsec), 0);
strictEqual(dyear.cmp(dsec), 0);
strictEqual(dyear.cmp(dmin), 0);
strictEqual(dyear.cmp(dhour), 0);
strictEqual(dyear.cmp(dday), 0);
strictEqual(dyear.cmp(dmonth), 0);
strictEqual(dmsec.cmp(JIODate('2012-05-02 06:07:07')), +1);
strictEqual(dmsec.cmp(JIODate('2012-05-02 06:07:08')), 0);
strictEqual(dmsec.cmp(JIODate('2012-05-02 06:07:09')), -1);
});
}));
/*jslint indent: 2, maxlen: 120, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, jiodate, window, test, ok,
equal, deepEqual, sinon, start, stop, RSVP */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('complex_queries'), require('jiodate'));
}
module(complex_queries, jiodate);
}(['complex_queries', 'jiodate', 'qunit'], function (complex_queries, jiodate) {
"use strict";
module('Custom Key Queries with JIODate');
var noop = function () {
return; // use with RSVP.all
};
test('Stock comparison operators with year precision', function () {
var docList = function () {
return [
{'identifier': 'twenty ten', 'date': '2010-03-04T08:52:13.746Z'},
{'identifier': 'twenty eleven', 'date': '2011-03-04T08:52:13.746Z'},
{'identifier': 'twenty twelve', 'date': '2012-03-04T08:52:13.746Z'}
];
}, key_schema = {
key_set: {
date: {
read_from: 'date',
cast_to: jiodate.JIODate
}
}
}, query_list = [], promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
], 'Match with "date = 2011" (query tree form)');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '!=',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date != 2011" (query tree form)');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '<',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'}
], 'Match with "date < 2011" (query tree form)');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '<=',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
], 'Match with "date <= 2011" (query tree form)');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '>',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date > 2011" (query tree form)');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date',
operator: '>=',
value: '2011'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
], 'Match with "date >= 2011" (query tree form)');
})
);
query_list = [
[
'date: < "2011" OR date: "2012-03"',
[
{'date': '2010-03-04T08:52:13.746Z', 'identifier': 'twenty ten'},
{'date': '2012-03-04T08:52:13.746Z', 'identifier': 'twenty twelve'}
]
],
[
'date: >= "2011-01" AND date: != "2012-03-04T08:52:13.746Z"',
[
{'date': '2011-03-04T08:52:13.746Z', 'identifier': 'twenty eleven'}
]
]
];
query_list.forEach(function (o) {
var qs = o[0], expected = o[1];
promise.push(
complex_queries.QueryFactory.create(qs, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, expected, "Match with '" + qs + "' (parsed query string)");
})
);
});
query_list = [
'date < "2011"',
'date <= "2011"',
'date > "2011"',
'date >= "2011"'
];
query_list.forEach(function (qs) {
promise.push(
complex_queries.QueryFactory.create(qs, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
], "Match with an invalid parsed string " + qs + " should return empty list but not raise errors");
})
);
});
RSVP.all(promise).then(noop).always(start);
});
}));
/*jslint indent: 2, maxlen: 100, nomen: true */
/*global window, define, module, test_util, RSVP, jIO, local_storage, test, ok,
deepEqual, sinon, expect, stop, start, Blob, console */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
module(RSVP, jIO, local_storage);
}([
'rsvp',
'jio',
'localstorage',
'qunit'
], function (RSVP, jIO, local_storage) {
"use strict";
module("LocalStorage");
local_storage.clear();
var key_schema = {
cast_lookup: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
}
},
key_set: {
mydate: {
read_from: 'date',
cast_to: 'dateType'
}
}
};
test("AllDocs", function () {
expect(3);
var o = {}, jio = jIO.createJIO({
"type": "local",
"username": "ualldocs",
"application_name": "aalldocs",
"key_schema": key_schema
}, {
"workspace": {}
});
stop();
o.date_a = new Date(0);
o.date_b = new Date();
// put some document before listing them
RSVP.all([
jio.put({
"_id": "a",
"title": "one",
"date": o.date_a
}).then(function () {
return jio.putAttachment({
"_id": "a",
"_attachment": "aa",
"_data": "aaa"
});
}),
jio.put({"_id": "b", "title": "two", "date": o.date_a}),
jio.put({"_id": "c", "title": "one", "date": o.date_b}),
jio.put({"_id": "d", "title": "two", "date": o.date_b})
]).then(function () {
// get a list of documents
return jio.allDocs();
}).always(function (answer) {
// sort answer rows for comparison
if (answer.data && answer.data.rows) {
answer.data.rows.sort(function (a, b) {
return a.id < b.id ? -1 : a.id > b.id ? 1 : 0;
});
}
deepEqual(answer, {
"data": {
"rows": [{
"id": "a",
"key": "a",
"value": {}
}, {
"id": "b",
"key": "b",
"value": {}
}, {
"id": "c",
"key": "c",
"value": {}
}, {
"id": "d",
"key": "d",
"value": {}
}],
"total_rows": 4
},
"method": "allDocs",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "AllDocs");
}).then(function () {
// get a list of documents
return jio.allDocs({
"include_docs": true,
"sort_on": [['title', 'ascending'], ['date', 'descending']],
"select_list": ['title', 'date'],
"limit": [1] // ==> equal [1, 3] in this case
});
}).always(function (answer) {
deepEqual(answer, {
"data": {
"rows": [{
"doc": {
"_attachments": {
"aa": {
"content_type": "",
"digest": "sha256-9834876dcfb05cb167a5c24953eba58c4" +
"ac89b1adf57f28f2f9d09af107ee8f0",
"length": 3
}
},
"_id": "a",
"date": o.date_a.toJSON(),
"title": "one"
},
"id": "a",
"key": "a",
"value": {
"date": o.date_a.toJSON(),
"title": "one"
}
}, {
"doc": {
"_id": "d",
"date": o.date_b.toJSON(),
"title": "two"
},
"id": "d",
"key": "d",
"value": {
"date": o.date_b.toJSON(),
"title": "two"
}
}, {
"doc": {
"_id": "b",
"date": o.date_a.toJSON(),
"title": "two"
},
"id": "b",
"key": "b",
"value": {
"date": o.date_a.toJSON(),
"title": "two"
}
}],
"total_rows": 3
},
"method": "allDocs",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "AllDocs include docs + sort on + limit + select_list");
}).then(function () {
// use a query
return jio.allDocs({'query': {
type: 'simple',
key: 'mydate',
operator: '=',
value: o.date_a.toString()
}});
}).always(function (answer) {
deepEqual(answer, {
"data": {
"rows": [{
"id": "a",
"key": "a",
"value": {}
}, {
"id": "b",
"key": "b",
"value": {}
}],
"total_rows": 2
},
"method": "allDocs",
"result": "success",
"status": 200,
"statusText": "Ok"
}, "AllDocs sort on + query");
}).always(start);
});
}));
/*jslint indent: 2, maxlen: 100, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, window, test, ok,
deepEqual, sinon, start, stop, RSVP */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('complex_queries'));
}
module(complex_queries);
}(['complex_queries', 'qunit'], function (complex_queries) {
"use strict";
module('Custom Key Queries with Schema');
var noop = function () {
return; // use with RSVP.all
};
var translationEqualityMatcher = function (data) {
return function (object_value, value) {
value = data[value];
return (object_value === value);
};
};
/*jslint unparam: true*/
var key_schema = {
cast_lookup: {
dateType: function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
}
},
match_lookup: {
sameDay: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth()) &&
(a.getDate() === b.getDate())
);
},
sameMonth: function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth())
);
},
sameYear: function (a, b) {
return (a.getFullYear() === b.getFullYear());
},
equalState: translationEqualityMatcher({'ouvert': 'open'})
},
key_set: {
case_insensitive_identifier: {
read_from: 'identifier',
equal_match: function (object_value, value, wildcard_character) {
// XXX do this with a regexp and wildcard support
return (object_value.toLowerCase() === value.toLowerCase());
}
},
date_day: {
read_from: 'date',
cast_to: 'dateType',
equal_match: 'sameDay'
},
date_month: {
read_from: 'date',
cast_to: 'dateType',
equal_match: 'sameMonth'
},
date_year: {
read_from: 'date',
cast_to: 'dateType',
equal_match: 'sameYear'
},
translated_state: {
read_from: 'state',
equal_match: 'equalState'
}
}
};
/*jslint unparam: false*/
test('Keys defined in a Schema can be used like metadata', function () {
var docList = function () {
return [
{'identifier': 'a'},
{'identifier': 'A'},
{'identifier': 'b'}
];
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'case_insensitive_identifier',
value: 'A'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': 'a'},
{'identifier': 'A'}
], 'Key Schema: case_insensitive_identifier');
})
);
RSVP.all(promise).then(noop).always(start);
});
test('Standard date keys', function () {
var docList = function () {
return [
{'identifier': 'a', 'date': '2013-01-01'},
{'identifier': 'b', 'date': '2013-02-01'},
{'identifier': 'bb', 'date': '2013-02-02'},
{'identifier': 'bbb', 'date': '2013-02-03'},
{'identifier': 'c', 'date': '2013-03-03'},
{'identifier': 'd', 'date': '2013-04-04'}
];
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date_day',
value: '2013-02-02'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': 'bb', 'date': '2013-02-02'}
], 'Key Schema: same_day');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date_month',
value: '2013-02-10'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2013-02-01', 'identifier': 'b'},
{'date': '2013-02-02', 'identifier': 'bb'},
{'date': '2013-02-03', 'identifier': 'bbb'}
], 'Key Schema: date_month');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'date_year',
value: '2013-02-10'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl.length, 6, 'Key Schema: date_year');
})
);
RSVP.all(promise).then(noop).always(start);
});
test('Test key schema + complex queries', function () {
var docList = function () {
return [
{'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'},
{'identifier': '100', 'number': '100'}
];
}, key_schema = {
cast_lookup: {
intType: function (value) {
if (typeof value === 'string') {
return parseInt(value, 10);
}
return value;
}
},
key_set: {
number: {
read_from: 'number',
cast_to: 'intType'
}
}
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'complex',
operator: 'OR',
query_list: [{
type: 'simple',
key: 'number',
operator: '<',
value: '19'
}, {
type: 'simple',
key: 'number',
operator: '=',
value: '19'
}]
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'}
], 'Key schema should be propagated from complex to simple queries');
})
);
RSVP.all(promise).then(noop).always(start);
});
test('Key Schema with translation lookup', function () {
var docList = function () {
return [
{'identifier': '1', 'state': 'open'},
{'identifier': '2', 'state': 'closed'}
];
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'translated_state',
value: 'ouvert'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'state': 'open'}
], 'Key Schema: It should be possible to look for a translated string');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: 'translated_state',
operator: '=',
value: 'ouvert'
}, key_schema).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'state': 'open'}
], 'Key Schema: It should be possible to look for a translated string with operator =');
})
);
// XXX not implemented yet
// doc_list = docList();
// complex_queries.QueryFactory.create({
// type: 'simple',
// key: 'translated_state',
// operator: '!=',
// value: 'ouvert'
// }).exec(doc_list);
// deepEqual(doc_list, [
// {'identifier': '2', 'state': 'closed'}
// ], 'Key Schema: It should be possible to look for a translated string with operator !=');
RSVP.all(promise).then(noop).always(start);
});
}));
/*jslint indent: 2, maxlen: 90, nomen: true */
/*global define, exports, require, module, complex_queries, window, test,
raises, ok, equal, deepEqual, sinon */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('complex_queries'));
}
module(complex_queries);
}(['complex_queries', 'qunit'], function (complex_queries) {
"use strict";
module('Key and key_schema objects validation');
test('Check the parameters passed to exec() and create()', function () {
try {
complex_queries.QueryFactory.create('').exec('gnegne');
ok(false, 'argument 1 not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"Query().exec(): Argument 1 is not of type 'array'",
'wrong exception message');
}
try {
complex_queries.QueryFactory.create({});
ok(false, 'argument 1 not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"QueryFactory.create(): Argument 1 is not a search text or a parsable object",
'wrong exception message');
}
try {
complex_queries.QueryFactory.create('').exec([], 1);
ok(false, 'argument 2 not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"Query().exec(): Optional argument 2 is not of type 'object'",
'wrong exception message');
}
try {
complex_queries.QueryFactory.create({type: 'simple'}, '');
ok(false, 'key_schema type is not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"SimpleQuery().create(): key_schema is not of type 'object'",
'wrong exception message');
}
try {
complex_queries.QueryFactory.create({type: 'simple'}, {});
ok(false, 'key_schema.key_set is not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"SimpleQuery().create(): key_schema has no 'key_set' property",
'wrong exception message');
}
try {
complex_queries.QueryFactory.create({type: 'simple'}, {key_set: {}, foobar: {}});
ok(false, 'unknown key_schema properties are not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"SimpleQuery().create(): key_schema has unknown property 'foobar'",
'wrong exception message');
}
});
test('Check the key options', function () {
var doc_list = [
{'identifier': 'a'}
];
try {
complex_queries.QueryFactory.create({
type: 'simple',
key: {},
value: 'a'
}).exec(doc_list);
ok(false, 'key.read_from is not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"Custom key is missing the read_from property",
'wrong exception message');
}
try {
complex_queries.QueryFactory.create({
type: 'simple',
key: {
read_from: 'identifier',
foobar: ''
},
value: 'a'
}).exec(doc_list);
ok(false, 'unknown key properties are not checked');
} catch (e) {
equal(e.name, 'TypeError', 'wrong exception type');
equal(e.message,
"Custom key has unknown property 'foobar'",
'wrong exception message');
}
});
}));
/*jslint indent: 2, maxlen: 120, nomen: true, vars: true */
/*global define, exports, require, module, complex_queries, window, test, ok,
equal, deepEqual, sinon, stop, start, RSVP */
// define([module_name], [dependencies], module);
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
return module(require('complex_queries'));
}
module(complex_queries);
}(['complex_queries', 'qunit'], function (complex_queries) {
"use strict";
module('Custom Key Queries');
var noop = function () {
return; // use with RSVP.all
};
test('Simple Key with read_from', function () {
/*jslint unparam: true*/
var docList = function () {
return [
{'identifier': 'a'},
{'identifier': 'A'},
{'identifier': 'b'}
];
}, keys = {
title: {
read_from: 'identifier'
},
case_insensitive_identifier: {
read_from: 'identifier',
equal_match: function (object_value, value, wildcard_character) {
return (object_value.toLowerCase() === value.toLowerCase());
}
}
}, promise = [];
/*jslint unparam: false*/
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.title,
value: 'a'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': 'a'}
], 'It should be possible to query with an alias key');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.case_insensitive_identifier,
value: 'A'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': 'a'},
{'identifier': 'A'}
], 'It should be possible to query with a case-insensitive alias key');
})
);
RSVP.all(promise).then(noop).always(start);
});
var dateCast = function (obj) {
if (Object.prototype.toString.call(obj) === '[object Date]') {
// no need to clone
return obj;
}
return new Date(obj);
};
test('Simple Key with date casting', function () {
var docList = function () {
return [
{'identifier': 'a', 'date': '2013-01-01'},
{'identifier': 'b', 'date': '2013-02-01'},
{'identifier': 'bb', 'date': '2013-02-02'},
{'identifier': 'bbb', 'date': '2013-02-03'},
{'identifier': 'c', 'date': '2013-03-03'},
{'identifier': 'd', 'date': '2013-04-04'}
];
}, promise = [];
var sameDay = function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth()) &&
(a.getDate() === b.getDate())
);
};
var sameMonth = function (a, b) {
return (
(a.getFullYear() === b.getFullYear()) &&
(a.getMonth() === b.getMonth())
);
};
var sameYear = function (a, b) {
return (a.getFullYear() === b.getFullYear());
};
var keys = {
day: {
read_from: 'date',
cast_to: dateCast,
equal_match: sameDay
},
month: {
read_from: 'date',
cast_to: dateCast,
equal_match: sameMonth
},
year: {
read_from: 'date',
cast_to: dateCast,
equal_match: sameYear
}
};
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.day,
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': 'bb', 'date': '2013-02-02'}
], 'It should be possible to compare dates with sameDay');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.month,
value: '2013-02-10'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'date': '2013-02-01', 'identifier': 'b'},
{'date': '2013-02-02', 'identifier': 'bb'},
{'date': '2013-02-03', 'identifier': 'bbb'}
], 'It should be possible to compare dates with sameMonth');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.year,
value: '2013-02-10'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl.length, 6,
'It should be possible to compare dates with sameYear');
})
);
RSVP.all(promise).then(noop).always(start);
});
test('Simple Key with date casting and <=> operators', function () {
var docList = function () {
return [
{'identifier': '1', 'date': '2013-01-01'},
{'identifier': '2', 'date': '2013-02-02'},
{'identifier': '3', 'date': '2013-03-03'}
];
}, keys = {
mydate: {
read_from: 'date',
cast_to: dateCast
}
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '=',
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '2', 'date': '2013-02-02'}
], 'It should be possible to search for dates with operator =');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '!=',
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'date': '2013-01-01'},
{'identifier': '3', 'date': '2013-03-03'}
], 'It should be possible to search for dates with operator !=');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '<=',
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'date': '2013-01-01'},
{'identifier': '2', 'date': '2013-02-02'}
], 'It should be possible to search for dates with operator <=');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '<',
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'date': '2013-01-01'}
], 'It should be possible to search for dates with operator <');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '>',
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '3', 'date': '2013-03-03'}
], 'It should be possible to search for dates with operator >');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '>=',
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '2', 'date': '2013-02-02'},
{'identifier': '3', 'date': '2013-03-03'}
], 'It should be possible to search for dates with operator >=');
})
);
RSVP.all(promise).then(noop).always(start);
});
test('Simple Key with both equal_match and operator attributes', function () {
var docList = function () {
return [
{'identifier': '1', 'date': '2013-01-01'},
{'identifier': '2', 'date': '2013-02-02'},
{'identifier': '3', 'date': '2013-03-03'}
];
}, keys = {
mydate: {
read_from: 'date',
cast_to: dateCast,
equal_match: function alwaysTrue(o1) { /*, o2*/
return o1.getDate() === 2;
}
}
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '2', 'date': '2013-02-02'}
], "'equal_match' with no 'operator'");
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '=',
value: '2013-01-01'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '2', 'date': '2013-02-02'}
], "'equal_match' overrides '=' operator");
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.mydate,
operator: '>=',
value: '2013-02-02'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '2', 'date': '2013-02-02'},
{'identifier': '3', 'date': '2013-03-03'}
], "'equal_match' does not override '>' operator");
})
);
RSVP.all(promise).then(noop).always(start);
});
test('Test overriding operators and compound query', function () {
var docList = function () {
return [
{'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'},
{'identifier': '100', 'number': '100'}
];
}, intType = function (value) {
if (typeof value === 'string') {
return parseInt(value, 10);
}
return value;
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: {
read_from: 'number',
cast_to: intType
},
operator: '>',
value: '19'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '100', 'number': '100'}
], 'Numbers are correctly compared (>) after casting');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: {
read_from: 'number',
cast_to: intType
},
operator: '<',
value: '19'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '10', 'number': '10'}
], 'Numbers are correctly compared (<) after casting');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'complex',
operator: 'OR',
query_list: [{
type: 'simple',
key: {
read_from: 'number',
cast_to: intType
},
operator: '<',
value: '19'
}, {
type: 'simple',
key: {
read_from: 'number',
cast_to: intType
},
operator: '=',
value: '19'
}]
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '10', 'number': '10'},
{'identifier': '19', 'number': '19'}
], 'Custom keys should also work within compound queries');
})
);
RSVP.all(promise).then(noop).always(start);
});
var translationEqualityMatcher = function (data) {
return function (object_value, value) {
value = data[value];
return (object_value === value);
};
};
test('Simple Key with translation lookup', function () {
var docList = function () {
return [
{'identifier': '1', 'state': 'open'},
{'identifier': '2', 'state': 'closed'}
];
},
equalState = translationEqualityMatcher({'ouvert': 'open'}),
keys = {
translated_state: {
read_from: 'state',
equal_match: equalState
}
}, promise = [];
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.translated_state,
value: 'ouvert'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'state': 'open'}
], 'It should be possible to look for a translated string with a custom match function');
})
);
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.translated_state,
operator: '=',
value: 'ouvert'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': '1', 'state': 'open'}
], 'It should be possible to look for a translated string with operator =');
})
);
// XXX not implemented yet
// doc_list = docList();
// complex_queries.QueryFactory.create({
// type: 'simple',
// key: keys.translated_state,
// operator: '!=',
// value: 'ouvert'
// }).exec(doc_list);
// deepEqual(doc_list, [
// {'identifier': '2', 'state': 'closed'}
// ], 'It should be possible to look for a translated string with operator !=');
RSVP.all(promise).then(noop).always(start);
});
// This method is provided as an example.
// A more robust solution to manage diacritics is recommended for production
// environments, with unicode normalization, like (untested):
// https://github.com/walling/unorm/
var accentFold = function (s) {
var map = [
[new RegExp('[àáâãäå]', 'gi'), 'a'],
[new RegExp('æ', 'gi'), 'ae'],
[new RegExp('ç', 'gi'), 'c'],
[new RegExp('[èéêë]', 'gi'), 'e'],
[new RegExp('[ìíîï]', 'gi'), 'i'],
[new RegExp('ñ', 'gi'), 'n'],
[new RegExp('[òóôõö]', 'gi'), 'o'],
[new RegExp('œ', 'gi'), 'oe'],
[new RegExp('[ùúûü]', 'gi'), 'u'],
[new RegExp('[ýÿ]', 'gi'), 'y']
];
map.forEach(function (o) {
var rep = function (match) {
if (match.toUpperCase() === match) {
return o[1].toUpperCase();
}
return o[1];
};
s = s.replace(o[0], rep);
});
return s;
};
test('Accent folding', function () {
equal(accentFold('àéîöùç'), 'aeiouc');
equal(accentFold('ÀÉÎÖÙÇ'), 'AEIOUC');
equal(accentFold('àéî öùç'), 'aei ouc');
});
test('Query with accent folding and wildcard', function () {
/*jslint unparam: true*/
var docList = function () {
return [
{'identifier': 'àéîöùç'},
{'identifier': 'âèî ôùc'},
{'identifier': 'ÀÉÎÖÙÇ'},
{'identifier': 'b'}
];
}, keys = {
identifier: {
read_from: 'identifier',
cast_to: accentFold
}
}, promise = [];
/*jslint unparam: false*/
stop();
promise.push(
complex_queries.QueryFactory.create({
type: 'simple',
key: keys.identifier,
value: 'aei%'
}).
exec(docList()).
then(function (dl) {
deepEqual(dl, [
{'identifier': 'àéîöùç'},
{'identifier': 'âèî ôùc'}
], 'It should be possible to query regardless of accents');
})
);
RSVP.all(promise).then(noop).always(start);
});
}));
......@@ -19,9 +19,18 @@
<script src="jio/tests.js"></script>
<script src="../complex_queries.js"></script>
<script src="queries/key.tests.js"></script>
<script src="queries/key-schema.tests.js"></script>
<script src="queries/tests.js"></script>
<script src="queries/key-typechecks.tests.js"></script>
<script src="../lib/moment/moment-2.5.0.js"></script>
<script src="../src/jio.date/jiodate.js"></script>
<script src="queries/jiodate.tests.js"></script>
<script src="queries/key-jiodate.tests.js"></script>
<script src="../src/jio.storage/localstorage.js"></script>
<script src="queries/key-localstorage.tests.js"></script>
<script src="jio.storage/localstorage.tests.js"></script>
<script src="../src/jio.storage/davstorage.js"></script>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment