Skip to content

Commit

Permalink
Removing a truthy, moving RegExp into regex
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jun 29, 2017
1 parent 7b665ac commit ed335f4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function parse (req, res, next) {

function keymaster (req, res, next) {
const obj = req.server.tenso,
authd = req.session && req.isAuthenticated();
authd = req.session !== void 0 && req.isAuthenticated();

// No authentication, or it's already happened
if (!req.protect || !req.protectAsync || authd) {
Expand Down
6 changes: 5 additions & 1 deletion lib/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ const path = require("path"),
encode_json: /application\/json/,
get_rewrite: /HEAD|OPTIONS/,
has_param: /\/:(\w*)/,
has_order_by: /(\?|&)order_by/,
has_order_by_desc: /(\?|&)order_by=desc/,
json_wrap: /^[\[\{"]/,
leading: /.*\//,
mimetype: /;.*/,
modify: /DELETE|PATCH|POST|PUT/,
options: /OPTIONS/,
order_by: /^order_by\=/,
private: /private/,
scheme: /^(\w+\:\/\/)|\//,
trailing: /_.*$/,
trailing_s: /s$/,
trailing_slash: /\/$/,
trailing_y: /y$/
trailing_y: /y$/,
unsortable: /boolean|number|string|void 0/
};

iterate(cfg.regex || {}, (value, key) => {
Expand Down
15 changes: 6 additions & 9 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ const path = require("path"),
regex = require(path.join(__dirname, "regex.js")),
utility = require(path.join(__dirname, "utility.js")),
renderers = require(path.join(__dirname, "renderers")),
serializers = require(path.join(__dirname, "serializers")),
hasOrderBy = /(\?|&)order_by/,
hasOrderByDesc = /(\?|&)order_by=desc/,
orderBy = /^order_by\=/;
serializers = require(path.join(__dirname, "serializers"));

class Tenso {
constructor (config = {headers: {}}) {
Expand Down Expand Up @@ -192,18 +189,18 @@ class Tenso {
sort (arg, req) {
let output;

if (typeof req.parsed.search === "string" && hasOrderBy.test(req.parsed.search) && Array.isArray(arg)) {
if (!(/boolean|number|string|void 0/).test(typeof arg[0]) && arg[0] !== null) {
output = keysort(utility.clone(arg), req.parsed.search.replace("?", "").split("&").filter(i => orderBy.test(i)).reduce((a, b) => {
a.push(b.replace(orderBy, ""));
if (typeof req.parsed.search === "string" && regex.has_order_by.test(req.parsed.search) && Array.isArray(arg)) {
if (!regex.unsortable.test(typeof arg[0]) && arg[0] !== null) {
output = keysort(utility.clone(arg), req.parsed.search.replace("?", "").split("&").filter(i => regex.order_by.test(i)).reduce((a, b) => {
a.push(b.replace(regex.order_by, ""));

return a;
}, []).join(", ").replace(/\%20|\+/g, " "));
} else {
// Primitives, regular Array.sort()
output = utility.clone(arg).sort(retsu.sort);

if (hasOrderByDesc.test(req.parsed.search)) {
if (regex.has_order_by_desc.test(req.parsed.search)) {
output = output.reverse();
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is an elastic REST API gateway for node.js",
"version": "4.5.24",
"version": "4.5.25",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down

0 comments on commit ed335f4

Please sign in to comment.