Skip to content

Commit a696251

Browse files
committed
typescript
1 parent 3a16aec commit a696251

File tree

5 files changed

+143
-129
lines changed

5 files changed

+143
-129
lines changed

packages/ns/index.d.ts

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/ns/index.js

Lines changed: 0 additions & 97 deletions
This file was deleted.

packages/ns/src/index.spec.ts

Whitespace-only changes.

packages/ns/test.js renamed to packages/ns/src/index.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var assert = require('assert');
2-
var ns = require('./');
2+
var ns = require('..');
33

44
describe('ns', function() {
55
describe('normal', function() {

packages/ns/src/index.ts

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
type 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+
// Assigned in the constructor, but will always be undefined
22+
isConf(): undefined;
23+
};
24+
25+
type NSConstructor = {
26+
new (ns: string | NS): NS;
27+
prototype: NS;
28+
MAX_DATABASE_NAME_LENGTH: number;
29+
sort(namespaces: (string | NS)[]): typeof namespaces;
30+
};
31+
32+
// eslint-disable-next-line complexity
33+
const NS: NSConstructor = function (this: NS, ns: string | NS): NS {
34+
ns = ns.toString();
35+
if (!(this instanceof NS)) {
36+
return new NS(ns);
37+
}
38+
39+
this.ns = ns;
40+
this.dotIndex = ns.indexOf('.');
41+
if (this.dotIndex === -1) {
42+
this.database = ns;
43+
this.collection = '';
44+
} else {
45+
this.database = ns.slice(0, this.dotIndex);
46+
this.collection = ns.slice(this.dotIndex + 1);
47+
}
48+
49+
this.system = /^(?:system(?!\.profile$).*|enxcol_)\./.test(this.collection);
50+
51+
this.oplog = /local\.oplog\.(\$main|rs)/.test(ns);
52+
53+
this.command =
54+
this.collection === '$cmd' || this.collection.indexOf('$cmd.sys') === 0;
55+
this.special =
56+
this.oplog ||
57+
this.command ||
58+
this.system ||
59+
this.database === 'config' ||
60+
/^__mdb_internal_\w/.test(this.database);
61+
62+
this.specialish =
63+
this.special || ['local', 'admin'].indexOf(this.database) > -1;
64+
65+
this.normal = this.oplog || this.ns.indexOf('$') === -1;
66+
67+
/**
68+
* @note (imlucas) The following are not valid on windows:
69+
* `*<>:|?`
70+
*/
71+
this.validDatabaseName =
72+
new RegExp('^[^\\\\/". ]*$').test(this.database) &&
73+
this.database.length <= NS.MAX_DATABASE_NAME_LENGTH;
74+
this.validCollectionName =
75+
this.collection.length > 0 &&
76+
(this.oplog || /^[^\0$]*$/.test(this.collection));
77+
78+
this.databaseHash = 7;
79+
this.ns.split('').every((c, i) => {
80+
if (c === '.') {
81+
return false;
82+
}
83+
this.databaseHash += 11 * this.ns.charCodeAt(i);
84+
this.databaseHash *= 3;
85+
return true;
86+
});
87+
return this;
88+
} as unknown as NSConstructor;
89+
90+
NS.prototype.database = '';
91+
NS.prototype.databaseHash = 0;
92+
NS.prototype.collection = '';
93+
94+
NS.prototype.command = false;
95+
NS.prototype.special = false;
96+
NS.prototype.system = false;
97+
NS.prototype.oplog = false;
98+
NS.prototype.normal = false;
99+
NS.prototype.specialish = false;
100+
101+
NS.prototype.isCommand = function () {
102+
return this.command;
103+
};
104+
NS.prototype.isSpecial = function () {
105+
return this.special;
106+
};
107+
NS.prototype.isSystem = function () {
108+
return this.system;
109+
};
110+
NS.prototype.isNormal = function () {
111+
return this.normal;
112+
};
113+
NS.prototype.isOplog = function () {
114+
return this.oplog;
115+
};
116+
NS.prototype.isConf = function () {
117+
return undefined;
118+
};
119+
120+
NS.prototype.toString = function () {
121+
return this.ns;
122+
};
123+
124+
NS.MAX_DATABASE_NAME_LENGTH = 128;
125+
126+
NS.sort = function (namespaces: (NS | string)[]) {
127+
namespaces.sort(function (a, b) {
128+
if (new NS(a).specialish && new NS(b).specialish) {
129+
return 0;
130+
}
131+
if (new NS(a).specialish && !new NS(b).specialish) {
132+
return 1;
133+
}
134+
if (!new NS(a).specialish && new NS(b).specialish) {
135+
return -1;
136+
}
137+
return a > b ? 1 : -1;
138+
});
139+
return namespaces;
140+
};
141+
142+
export = NS;

0 commit comments

Comments
 (0)