Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Webpack/AMD support #72

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions js-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* require("./js-array").query("a=3", {}, [{a:1},{a:3}]) -> [{a:3}]
*
*/

({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory(exports, require("./parser"), require("./query"), require("./util/each"), require("./util/contains"));}}).
if (typeof define !== 'function') { var define = require('amdefine')(module) }
define(["exports", "./parser", "./query", "./util/each", "./util/contains"], function(exports, parser, QUERY, each, contains){
//({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory(exports, require("./parser"));}}).
//define(["exports", "./parser"], function(exports, parser){
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"promised-io": "http://github.com/kriszyp/promised-io/zipball/v0.2.1"
},
"dependencies": {
"amdefine": "^1.0.0",
"promised-io": ">=0.3.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This module provides RQL parsing. For example:
* var parsed = require("./parser").parse("b=3&le(c,5)");
*/
({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory(exports, require("./util/contains"));}}).
if (typeof define !== 'function') { var define = require('amdefine')(module) }
define(["exports", "./util/contains"], function(exports, contains){

var operatorMap = {
Expand Down
77 changes: 49 additions & 28 deletions query.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
* // for each object that matches the query
* });
*/
//({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory(exports, require("./parser"), require("./js-array"));}}).
//define(["exports", "./parser", "./js-array"], function(exports, parser, jsarray){
({define:typeof define!="undefined"?define:function(deps, factory){module.exports = factory(exports, require("./parser"), require("./util/each"));}}).
if (typeof define !== 'function') { var define = require('amdefine')(module) }
define(["exports", "./parser", "./util/each"], function(exports, parser, each){

var parseQuery = parser.parseQuery;
Expand Down Expand Up @@ -78,33 +76,56 @@ function encodeString(s) {
return s;
}

/**
* Encode a value of a part's argument for use in an RQL string.
* @param {*} val - The value to encode.
* @returns {string} The encoded value, URI-safe.
*/
exports.encodeValue = function(val) {
var encoded;
if (val === null) val = 'null';
if (val !== parser.converters["default"]('' + (
val.toISOString && val.toISOString() || val.toString()
))) {
var type = typeof val;
if(val instanceof RegExp){
// TODO: control whether to we want simpler glob() style
val = val.toString();
var i = val.lastIndexOf('/');
type = val.substring(i).indexOf('i') >= 0 ? "re" : "RE";
val = encodeString(val.substring(1, i));
encoded = true;
}
if(type === "object"){
type = "epoch";
val = val.getTime();
encoded = true;
}
if(type === "string") {
val = encodeString(val);
encoded = true;
}
val = [type, val].join(":");
// Remember whether any encoding took place. The function force-encodes
// the value as a plain string at the end as a last resort.
var encoded = false;

// Try encoding the value. If it throws, go to our failsafe.
try {
// Check whether casting the value to a string and parsing it would
// result in the input value (i.e. if the value can be left prefix-less).
var stringVal = '' + ((val && val.toISOString) ? val.toISOString() : val);
var parsedStringVal = parser.converters["default"](stringVal);
if (val === parsedStringVal) {
return encodeString(String(val));
}
// Now, only non-trivial cases are left:
var type = typeof val;
if (val instanceof RegExp) {
// TODO: control whether to we want simpler glob() style
val = val.toString();
var i = val.lastIndexOf('/');
type = val.substring(i).indexOf('i') >= 0 ? "re" : "RE";
val = encodeString(val.substring(1, i));
encoded = true;
}
if (type === "object") {
// Assume it's a date - it's the only other object type we support:
type = "epoch";
val = val.getTime();
encoded = true;
}
if (type === "string") {
// It's a string, but it doesn't parse to its own exact value.
// This happens when the input is a String('null') for example -
// the auto-converter parses null to type null, so we need a prefix.
val = encodeString(val);
encoded = true;
}
val = [type, val].join(":");
} catch (conversionError) {
// The conversion necessity check has failed and we have not encoded the value. The next conditional block will take care of it.
}
if (!encoded && typeof val === "string") {
val = encodeString(val);
}
if (!encoded && typeof val === "string") val = encodeString(val);
val = String(val);
return val;
};

Expand Down
2 changes: 1 addition & 1 deletion util/contains.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
({define:typeof define!=='undefined'?define:function(deps, factory){module.exports = factory(exports);}}).
if (typeof define !== 'function') { var define = require('amdefine')(module) }
define([], function(){
return contains;

Expand Down
2 changes: 1 addition & 1 deletion util/each.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
({define:typeof define!=='undefined'?define:function(deps, factory){module.exports = factory(exports);}}).
if (typeof define !== 'function') { var define = require('amdefine')(module) }
define([], function(){
return each;

Expand Down