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

Added minimal_quotes feature #220

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ mongo
- Default indent is 2 spaces instead of tab (config: `indent`)
- Disable notification of "Type 'it' for more"
- Option to sort document keys (config: `sort_keys`)
- Option to format keys in JavaScript style (config: `javascript_keys`)
- Option to quote strings minimally (config: `minimal_quotes`)
- Custom prompt: `hostname(process-version)[rs_status:set_name] db>`
- Always pretty print. You can still use default format by appending `.ugly()` to the end of a statement.
- Colorized query output for console/terminal windows supporting ANSI color codes.
Expand Down Expand Up @@ -160,29 +162,29 @@ db.test.aggregate().group({_id: '$a', 'sum': {'$sum': 1}}).sort({sum: -1})

#### Data Generation

For easy and simple random data generation you can utilise these methods below. You can use any of these functions in a loop. For example:
For easy and simple random data generation you can utilise these methods below. You can use any of these functions in a loop. For example:

```js
// Inserts 20 documents with random data.
for (i=1; i<21; i++) {
// Inserts 20 documents with random data.
for (i=1; i<21; i++) {
db.collection.insert(
{
word: randomWord(),
number: randomNumber(),
date: randomDate()
word: randomWord(),
number: randomNumber(),
date: randomDate()
}
);
);
}
```

##### randomWord
##### randomWord

You can specify the length of each word, the number of words, and an optional seeded word in a sentence randomly. Use the optional `seed` parameter for testing text search.

`randomWord(length=5, words=1, seed=undefined)`
`randomWord(length=5, words=1, seed=undefined)`

```js
// Inserts a random sentence consisting of 5 letters per word, 5 words in total,
// Inserts a random sentence consisting of 5 letters per word, 5 words in total,
// with a probability to insert the word 'needle' in the sentence
db.collection.insert( { words: randomWord(5, 5, 'needle') } )

Expand All @@ -197,15 +199,15 @@ You can specify maximum number to be randomly generated (exclusive)
`randomNumber(max=100)`

```js
// Inserts a random number in the range of 0 or 1.
// Inserts a random number in the range of 0 or 1.
db.collection.insert( { number: randomNumber(2) } )

// Inserts a random number in the range of 0 or 999.
// Inserts a random number in the range of 0 or 999.
db.collection.insert( { number: randomNumber(1000) } )

```

##### randomDate
##### randomDate

You can specify start and end dates range to be randomly generated. (exclusive)

Expand All @@ -215,7 +217,7 @@ You can specify start and end dates range to be randomly generated. (exclusive)
// Inserts a random date object in the range of 1st January 2016 to 1st February 2016
db.collection.insert( { date: randomDate(ISODate("2016-01-01T00:00:00"), ISODate("2016-02-01T00:00:00")) })

// If today is 19th May 2016 and you specify only the start of the day,
// If today is 19th May 2016 and you specify only the start of the day,
// this will generate random date object between 00:00:00 to current time.
db.collection.insert( { date: randomDate(ISODate("2016-05-19T00:00:00")) })
```
Expand Down
3 changes: 2 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ mongo_hacker_config = {
enhance_api: true, // additonal api extensions
indent: 2, // number of spaces for indent
sort_keys: false, // sort the keys in documents when displayed
javascript_keys:false, // output is formatted with JavaScript style keys
minimal_quotes: false, // single quote strings that don't contain them
uuid_type: 'default', // 'java', 'c#', 'python' or 'default'
banner_message: 'Mongo-Hacker ', // banner message
version: '0.1.1', // current mongo-hacker version
Expand Down Expand Up @@ -53,4 +55,3 @@ mongo_hacker_config = {
if (mongo_hacker_config['show_banner']) {
print(mongo_hacker_config['banner_message'] + mongo_hacker_config['version']);
}

10 changes: 6 additions & 4 deletions hacks/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ tojsonObject = function( x, indent, nolint, nocolor, sort_keys ) {
continue;

var color = mongo_hacker_config.colors.key;
s += indent + colorize("\"" + key + "\"", color, nocolor) + ": " + tojson( val, indent , nolint, nocolor, sortKeys );
var formattedKey = mongo_hacker_config.javascript_keys && key.match(/^[A-Za-z_][A-Za-z\d_]*$/) ? key : '"' + key + '"';
s += indent + colorize(formattedKey, color, nocolor) + ": " + tojson( val, indent , nolint, nocolor, sortKeys );
if (num != total) {
s += ",";
num++;
Expand Down Expand Up @@ -294,10 +295,11 @@ tojson = function( x, indent , nolint, nocolor, sort_keys ) {
var s;
switch ( typeof x ) {
case "string": {
s = "\"";
var quoteChar = mongo_hacker_config.minimal_quotes && (x.indexOf("'")===-1 || x.indexOf('"')>-1) ? "'" : '"';
s = quoteChar;
for ( var i=0; i<x.length; i++ ){
switch (x[i]){
case '"': s += '\\"'; break;
case quoteChar: s += '\\' + quoteChar; break;
case '\\': s += '\\\\'; break;
case '\b': s += '\\b'; break;
case '\f': s += '\\f'; break;
Expand All @@ -315,7 +317,7 @@ tojson = function( x, indent , nolint, nocolor, sort_keys ) {
}
}
}
s += "\"";
s += quoteChar;
return colorize(s, mongo_hacker_config.colors.string, nocolor);
}
case "number":
Expand Down