sort-nested-json
Version:
A module to sort a JSON array.
163 lines (162 loc) • 4.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sort = exports.sortNullsFirst = exports.sortNullsLast = exports.sortWithMemory = void 0;
const sortWithMemory = (json) => {
const { result, order_by, direction } = json;
if ((order_by && direction === Direction.ASC) ||
direction === Direction.DESC) {
return {
run: (orderBy) => {
let newdirection = Direction.ASC;
if (order_by === orderBy && direction === Direction.ASC) {
newdirection = Direction.DESC;
}
const resultwithmemory = {
result: sortJSON(result, orderBy, newdirection),
order_by: orderBy,
direction: newdirection,
};
return resultwithmemory;
},
};
}
else {
return {
run: (orderBy) => {
const newdirection = Direction.ASC;
const resultwithmemory = {
result: sortJSON(json, orderBy, newdirection),
order_by: orderBy,
direction: newdirection,
};
return resultwithmemory;
},
};
}
};
exports.sortWithMemory = sortWithMemory;
const sort = (list) => {
return {
asc: (sortBy) => {
return sortJSON(list, sortBy, Direction.ASC);
},
desc: (sortBy) => {
return sortJSON(list, sortBy, Direction.DESC);
},
};
};
exports.sort = sort;
const sortNullsLast = (list) => {
return {
asc: (sortBy) => {
return sortJSON(list, sortBy, Direction.ASC, Position.LAST);
},
desc: (sortBy) => {
return sortJSON(list, sortBy, Direction.DESC, Position.LAST);
},
};
};
exports.sortNullsLast = sortNullsLast;
const sortNullsFirst = (list) => {
return {
asc: (sortBy) => {
return sortJSON(list, sortBy, Direction.ASC, Position.FIRST);
},
desc: (sortBy) => {
return sortJSON(list, sortBy, Direction.DESC, Position.FIRST);
},
};
};
exports.sortNullsFirst = sortNullsFirst;
const sortJSON = (list, key, order, nulls) => {
try {
list = JSON.parse(list);
}
catch (e) {
list = list;
}
const tempArray = [];
for (let item of list) {
const object = item;
let objectKey = "";
const keys = key.split(".");
for (const keyItem of keys) {
item = item[keyItem];
objectKey = item;
}
tempArray.push({ key: objectKey, object: object });
}
const sortedArray = tempArray.sort((a, b) => {
if (typeof a === Type.STRING) {
return compareStrings(a.key, b.key, order, nulls);
}
else if (typeof a === Type.NUMBER || typeof a === Type.OBJECT) {
return compareNumber(a.key, b.key, order, nulls);
}
return "";
});
const jsonNewArray = [];
for (const i in sortedArray) {
jsonNewArray.push(sortedArray[i].object);
}
return jsonNewArray;
};
const compareStrings = (a, b, order, nulls) => {
if (nulls != null && (a == null || b == null)) {
return handleNulls(a, b, nulls);
}
a = a.toLowerCase();
b = b.toLowerCase();
if (order.toLowerCase() === Direction.ASC) {
return a < b ? -1 : a > b ? 1 : 0;
}
if (order.toLowerCase() === Direction.DESC) {
return b < a ? -1 : b > a ? 1 : 0;
}
};
const compareNumber = (a, b, order, nulls) => {
if (nulls != null && (a == null || b == null)) {
return handleNulls(a, b, nulls);
}
if (order.toLowerCase() === Direction.ASC) {
return a < b ? -1 : a > b ? 1 : 0;
}
if (order.toLowerCase() === Direction.DESC) {
return b < a ? -1 : b > a ? 1 : 0;
}
return 0;
};
const handleNulls = (a, b, nulls) => {
let result = 0;
if (a == null && b != null) {
result = 1;
}
if (a != null && b == null) {
result = -1;
}
if (a == null && b == null) {
result = 0;
}
switch (nulls) {
case Position.LAST:
return result;
case Position.FIRST:
return result * -1;
}
};
var Position;
(function (Position) {
Position["FIRST"] = "first";
Position["LAST"] = "last";
})(Position || (Position = {}));
var Direction;
(function (Direction) {
Direction["ASC"] = "asc";
Direction["DESC"] = "desc";
})(Direction || (Direction = {}));
var Type;
(function (Type) {
Type["STRING"] = "string";
Type["OBJECT"] = "object";
Type["NUMBER"] = "number";
})(Type || (Type = {}));