Skip to content

Commit e336ceb

Browse files
committed
move-ns: try to move ns package
1 parent 9e6c5bb commit e336ceb

File tree

11 files changed

+6466
-1060
lines changed

11 files changed

+6466
-1060
lines changed

package-lock.json

Lines changed: 5999 additions & 1059 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ns/.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": [
3+
"mongodb-js/node"
4+
]
5+
}

packages/ns/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.DS_Store
2+
node_modules/
3+
.dist/
4+
.build/
5+
npm-debug.log

packages/ns/.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.travis.yml
2+
test.js
3+
.eslintrc

packages/ns/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Lucas Hrabovsky <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/ns/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# mongodb-ns
2+
3+
Handle dem namespaces like the kernel do.
4+
5+
```
6+
var ns = require('mongodb-ns');
7+
8+
var bacon = ns('canadian-things.songs-aboot-bacon');
9+
console.log(bacon.toString() + '\n', bacon);
10+
```
11+
12+
will output
13+
14+
```
15+
canadian-things.songs-aboot-bacon
16+
NS {
17+
ns: 'canadian-things.songs-aboot-bacon',
18+
dotIndex: 15,
19+
database: 'canadian-things',
20+
collection: 'songs-aboot-bacon',
21+
system: false,
22+
oplog: false,
23+
command: false,
24+
special: false,
25+
specialish: false,
26+
normal: true,
27+
validDatabaseName: true,
28+
validCollectionName: true,
29+
databaseHash: 23620443216 }
30+
```
31+
32+
33+
## license
34+
35+
MIT

packages/ns/index.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class NS {
2+
ns: string;
3+
dotIndex: number;
4+
database: string;
5+
collection: string;
6+
system: boolean;
7+
isSystem(): boolean;
8+
oplog: boolean;
9+
isOplog(): boolean;
10+
command: boolean;
11+
isCommand(): boolean;
12+
special: boolean;
13+
isSpecial(): boolean;
14+
specialish: boolean;
15+
normal: boolean;
16+
isNormal(): boolean;
17+
validDatabaseName: boolean;
18+
validCollectionName: boolean;
19+
databaseHash: number;
20+
toString(): string;
21+
static sort(namespaces: (string | NS)[]): typeof namespaces;
22+
static MAX_DATABASE_NAME_LENGTH: number;
23+
// Assigned in the constructor, but will always be undefined
24+
// isConf: undefined;
25+
}
26+
27+
declare const toNS: ((namespace: string | NS) => NS) & {
28+
sort: typeof NS['sort'];
29+
};
30+
31+
export default toNS;

packages/ns/index.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
var types = ['Command', 'Special', 'System', 'Oplog', 'Normal', 'Conf'];
2+
3+
// eslint-disable-next-line complexity
4+
function NS(ns) {
5+
if (!(this instanceof NS)) {
6+
return new NS(ns);
7+
}
8+
9+
this.ns = ns;
10+
this.dotIndex = ns.indexOf('.');
11+
if (this.dotIndex === -1) {
12+
this.database = ns;
13+
this.collection = '';
14+
} else {
15+
this.database = ns.slice(0, this.dotIndex);
16+
this.collection = ns.slice(this.dotIndex + 1);
17+
}
18+
19+
this.system = /^(?:system(?!\.profile$).*|enxcol_)\./.test(this.collection);
20+
21+
this.oplog = /local\.oplog\.(\$main|rs)/.test(ns);
22+
23+
this.command =
24+
this.collection === '$cmd' || this.collection.indexOf('$cmd.sys') === 0;
25+
this.special =
26+
this.oplog || this.command || this.system || this.database === 'config' || /^__mdb_internal_\w/.test(this.database);
27+
28+
this.specialish =
29+
this.special || ['local', 'admin'].indexOf(this.database) > -1;
30+
31+
this.normal = this.oplog || this.ns.indexOf('$') === -1;
32+
33+
/**
34+
* @note (imlucas) The following are not valid on windows:
35+
* `*<>:|?`
36+
*/
37+
this.validDatabaseName =
38+
new RegExp('^[^\\\\/". ]*$').test(this.database) &&
39+
this.database.length <= NS.MAX_DATABASE_NAME_LENGTH;
40+
this.validCollectionName =
41+
this.collection.length > 0 &&
42+
(this.oplog || /^[^\0\$]*$/.test(this.collection));
43+
44+
this.databaseHash = 7;
45+
this.ns.split('').every(
46+
function(c, i) {
47+
if (c === '.') {
48+
return false;
49+
}
50+
this.databaseHash += 11 * this.ns.charCodeAt(i);
51+
this.databaseHash *= 3;
52+
return true;
53+
}.bind(this)
54+
);
55+
}
56+
57+
NS.prototype.database = '';
58+
NS.prototype.databaseHash = 0;
59+
NS.prototype.collection = '';
60+
61+
NS.prototype.command = false;
62+
NS.prototype.special = false;
63+
NS.prototype.system = false;
64+
NS.prototype.oplog = false;
65+
NS.prototype.normal = false;
66+
NS.prototype.specialish = false;
67+
68+
types.forEach(function(type) {
69+
NS.prototype['is' + type] = function() {
70+
return this[type.toLowerCase()];
71+
};
72+
});
73+
74+
NS.prototype.toString = function() {
75+
return this.ns;
76+
};
77+
78+
NS.MAX_DATABASE_NAME_LENGTH = 128;
79+
80+
module.exports = NS;
81+
82+
var ns = NS;
83+
module.exports.sort = function(namespaces) {
84+
namespaces.sort(function(a, b) {
85+
if (ns(a).specialish && ns(b).specialish) {
86+
return 0;
87+
}
88+
if (ns(a).specialish && !ns(b).specialish) {
89+
return 1;
90+
}
91+
if (!ns(a).specialish && ns(b).specialish) {
92+
return -1;
93+
}
94+
return a > b ? 1 : -1;
95+
});
96+
return namespaces;
97+
};

packages/ns/package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "mongodb-ns",
3+
"version": "2.4.3",
4+
"author": {
5+
"name": "MongoDB Inc",
6+
"email": "[email protected]"
7+
},
8+
"description": "MongoDB namespace parsing and validation",
9+
"main": "./index.js",
10+
"types": "./index.d.ts",
11+
"scripts": {
12+
"test": "mocha",
13+
"check": "mongodb-js-precommit"
14+
},
15+
"homepage": "https://github.com/mongodb-js/devtools-shared",
16+
"repository": {
17+
"type": "git",
18+
"url": "git://github.com/mongodb-js/ns.git"
19+
},
20+
"keywords": [
21+
"mongodb",
22+
"mongodb.js"
23+
],
24+
"license": "MIT",
25+
"dependencies": {},
26+
"devDependencies": {
27+
"eslint-config-mongodb-js": "^5.0.3",
28+
"mocha": "^7.0.0",
29+
"mongodb-js-precommit": "^2.0.0"
30+
}
31+
}

0 commit comments

Comments
 (0)