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

Some minor improvements #9

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
27 changes: 16 additions & 11 deletions snippets/mongocompat/mongotypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ Date.prototype.tojson = function() {
ofs + '")';
};

ISODate = function(isoDateStr) {
if (buildInfo().version.split('.').reverse().map((x,i) => parseInt(x) * Math.pow(10,3*i)).reduce((sum, x) => sum + x, 0) < 2003001) {
// Not needed anymore in version 2.3.1 or later
ISODate = function(isoDateStr) {
if (!isoDateStr)
return new Date();

Expand Down Expand Up @@ -121,7 +123,8 @@ ISODate = function(isoDateStr) {
throw Error("invalid ISO date: " + isoDateStr);

return new Date(time);
};
};
}

// Regular Expression
RegExp.escape = function(text) {
Expand Down Expand Up @@ -414,12 +417,14 @@ ObjectId.prototype.tojson = function() {
return this.toString();
};

Object.defineProperty(ObjectId.prototype, 'str', {
enumerable: true,
get() {
return this.toHexString();
}
});
if (!ObjectId.prototype.hasOwnProperty('str')) {
Object.defineProperty(ObjectId.prototype, 'str', {
enumerable: true,
get() {
return this.toHexString();
}
});
}

ObjectId.prototype.valueOf = function() {
return this.str;
Expand Down Expand Up @@ -586,8 +591,8 @@ tojson = function(x, indent, nolint, depth) {
return s;
}
case "function":
if (x === MinKey || x === MaxKey)
return x.tojson();
if (x === MinKey) return tojson({ "$minKey" : 1 });
if (x === MaxKey) return tojson({ "$maxKey" : 1 });
return x.toString();
default:
throw Error("tojson can't handle type " + (typeof x));
Expand Down Expand Up @@ -648,7 +653,7 @@ tojsonObject = function(x, indent, nolint, depth) {
if (typeof DBCollection != 'undefined' && val == DBCollection.prototype)
continue;

fieldStrings.push(indent + "\"" + k + "\" : " + tojson(val, indent, nolint, depth + 1));
fieldStrings.push(indent + k + ": " + tojson(val, indent, nolint, depth + 1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks if k contains non-identifier characters, right? Just checking, this is already a bit broken either way (k couild probably also contain double quotes or backslashes)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it was more a change for my personal needs.
I think I reverted it in 7c83f98 - I am not the expert yet with git.

}

if (fieldStrings.length > 0) {
Expand Down