Skip to content

Commit 8e8bb24

Browse files
committed
Fixed unreachable code from NodeJS fixed, also making same interface for NodeJS and Bower
1 parent 5a73571 commit 8e8bb24

File tree

8 files changed

+266
-245
lines changed

8 files changed

+266
-245
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ Each example uses static import:
6464

6565
```js
6666
// nodejs
67-
var EidPreconditions = require('eid/preconditions');
67+
var EidPreconditions = require('eid.js').preconditions;
6868

6969
// browser using bower using toplevel version
70-
window.EidPreconditions;
70+
var EidPreconditions = window.Eid.preconditions;
7171
```
7272

7373
#### `checkArgument` method

gulp/build/toplevel.js

-7
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,5 @@
1616

1717
(function(wnd) {
1818
'use strict';
19-
2019
wnd.Eid = require('../../lib/eid');
21-
wnd.EidNullPointerException = require('../../lib/eid/exceptions').EidNullPointerException;
22-
wnd.EidIllegalArgumentException = require('../../lib/eid/exceptions').EidIllegalArgumentException;
23-
wnd.EidIllegalStateException = require('../../lib/eid/exceptions').EidIllegalStateException;
24-
wnd.EidIndexOutOfBoundsException = require('../../lib/eid/exceptions').EidIndexOutOfBoundsException;
25-
wnd.EidRuntimeException = require('../../lib/eid/exceptions').EidRuntimeException;
26-
wnd.EidPreconditions = require('../../lib/eid/preconditions');
2720
})(window);

lib/eid.js

+3-232
Original file line numberDiff line numberDiff line change
@@ -17,238 +17,9 @@
1717
(function(module) {
1818
'use strict';
1919

20-
function JFormatter(format) {
21-
this.format = function(arr) {
22-
var args = [];
23-
if (arguments.length === 1 && typeof(arr) === 'object') {
24-
args = arr;
25-
} else {
26-
for (var i = 0; i < arguments.length; i++) {
27-
args.push(arguments[i]);
28-
}
29-
}
30-
var regex = /%s/;
31-
var _r = function(p,c) { return p.replace(regex, c); }
32-
return args.reduce(_r, format);
33-
};
34-
}
35-
36-
/**
37-
* <strong>This class shouldn't be used in any public API or library.</strong> It is designed to be used for in-house development
38-
* of end user applications which will report Bugs in standardized error pages or post them to issue tracker.
39-
* <p>
40-
* Exception identifier for all Eid Runtime Exceptions.
41-
* @constructor
42-
* @param {string} id - the exception id, must be unique developer inserted string, from date
43-
* @param {string} ref - an optional reference
44-
*/
45-
function Eid(id, ref) {
46-
var uniq = EidInternal.uniqIdGenerator.generateUniqId();
47-
ref = (ref === null || ref == undefined) ? "" : ref;
48-
49-
/**
50-
* Makes a log message from this EID object
51-
* <p>
52-
* <p>This method is for convenience of usage of EID in logging. You can use it like this:
53-
* <p>
54-
* <pre>
55-
* log.debug(new Eid("20151025:202129").makeLogMessage("A request: %s", request));
56-
* </pre>
57-
* @param {string} logMessageFormat - a log message format as accepted by {@link String#format(String, Object...)}
58-
* @param {Object...} parameters - a parameters for logMessageFormat to by passed to {@link String#format(String, Object...)}
59-
* @return {string} a formatted message
60-
*/
61-
this.makeLogMessage = function(logMessageFormat) {
62-
var parameters = [];
63-
for (var i = 1; i < arguments.length; i++) {
64-
parameters.push(arguments[i]);
65-
}
66-
var message = new JFormatter(logMessageFormat).format(parameters);
67-
return new JFormatter(Eid.getMessageFormat()).format(this.toString(), message);
68-
}
69-
70-
/**
71-
* Standard to string method
72-
*/
73-
this.toString = function() {
74-
if ("" === ref) {
75-
return new JFormatter(EidInternal.format).format(id, uniq);
76-
}
77-
return new JFormatter(EidInternal.refFormat).format(id, ref, uniq);
78-
}
79-
80-
/**
81-
* Getter for constant Exception ID
82-
*
83-
* @return {string} ID of exception
84-
*/
85-
this.getId = function() {
86-
return id;
87-
}
88-
89-
/**
90-
* Get custom ref passed to Exception ID
91-
*
92-
* @return {string} ID of exception
93-
*/
94-
this.getRef = function() {
95-
return ref;
96-
}
97-
98-
/**
99-
* Gets unique generated string for this instance of Eid
100-
*
101-
* @return {string} a unique string
102-
*/
103-
this.getUniq = function() {
104-
return uniq;
105-
}
106-
}
107-
108-
function MathRandom() {
109-
var LONG_MAX = Math.pow(2, 53) - 1;
110-
var LONG_MIN = -1 * Math.pow(2, 53);
111-
this.nextLong = function() {
112-
return random(LONG_MIN, LONG_MAX);
113-
};
114-
this.nextInt = function(max) {
115-
return random(0, max);
116-
};
117-
var random = function(min, max) {
118-
return Math.floor(Math.random() * (max - min + 1)) + min;
119-
};
120-
}
121-
122-
function StdUniqIdGenerator() {
123-
var BASE36 = 36;
124-
var INTEGER_MAX_VALUE = Math.pow(2, 31);
125-
var generator = new MathRandom();
126-
this.generateUniqId = function() {
127-
var first = Math.abs(generator.nextLong() + 1);
128-
var second = Math.abs(generator.nextInt(INTEGER_MAX_VALUE));
129-
var calc = first + second;
130-
return (Math.abs(calc) % INTEGER_MAX_VALUE).toString(BASE36);
131-
};
132-
}
133-
134-
var DEFAULT_FORMAT = "[%s]<%s>";
135-
var DEFAULT_REF_FORMAT = "[%s|%s]<%s>";
136-
var DEFAULT_MESSAGE_FORMAT = "%s => %s";
137-
var DEFAULT_UNIQ_ID_GENERATOR = new StdUniqIdGenerator();
138-
139-
Object.defineProperty(Eid, "DEFAULT_FORMAT", {
140-
get: function() { return DEFAULT_FORMAT; }
141-
});
142-
Object.defineProperty(Eid, "DEFAULT_MESSAGE_FORMAT", {
143-
get: function() { return DEFAULT_MESSAGE_FORMAT; }
144-
});
145-
Object.defineProperty(Eid, "DEFAULT_REF_FORMAT", {
146-
get: function() { return DEFAULT_REF_FORMAT; }
147-
});
148-
Object.defineProperty(Eid, "DEFAULT_UNIQ_ID_GENERATOR", {
149-
get: function() { return DEFAULT_UNIQ_ID_GENERATOR; }
150-
});
151-
152-
var FORMAT_NUM_SPEC = 2;
153-
var REF_FORMAT_NUM_SPEC = 3;
154-
var MESSAGE_FORMAT_NUM_SPEC = 2;
155-
156-
var EidInternal = {
157-
messageFormat: DEFAULT_MESSAGE_FORMAT,
158-
uniqIdGenerator: DEFAULT_UNIQ_ID_GENERATOR,
159-
format: DEFAULT_FORMAT,
160-
refFormat: DEFAULT_REF_FORMAT
161-
};
162-
163-
var validateFormat = function(format, numSpecifiers) {
164-
if (format === null || format === undefined) {
165-
throw new TypeError("Format can't be null, but just received one");
166-
}
167-
var specifiers = [];
168-
for (var i = 0; i < numSpecifiers; i++) {
169-
specifiers.push(i + "-test-id");
170-
}
171-
var formatted = new JFormatter(format).format(specifiers);
172-
for (var specifier in specifiers) {
173-
if (formatted.indexOf(specifier) === -1) {
174-
throw new TypeError("Given format contains to little format specifiers, " +
175-
"expected " + numSpecifiers + " but given \"" + format + "\"");
176-
}
177-
}
178-
};
179-
180-
/**
181-
* Sets the actual unique ID generator that will be used to generate IDs for all Eid objects. It will return previously used
182-
* generator.
183-
*
184-
* @param {UniqIdGenerator} uniqIdGenerator - new instance of unique ID generator
185-
* @return {UniqIdGenerator} a previously used unique ID generator
186-
* @throws {TypeError} if given generator was null
187-
*/
188-
Eid.setUniqIdGenerator = function(uniqIdGenerator) {
189-
if (uniqIdGenerator === null || uniqIdGenerator === undefined) {
190-
throw new TypeError("Unique ID generator can't be null, but given one");
191-
}
192-
var previous = EidInternal.uniqIdGenerator;
193-
EidInternal.uniqIdGenerator = uniqIdGenerator;
194-
return previous;
195-
};
196-
197-
/**
198-
* Sets the actual format that will be used in {@link #toString()} method. It will return previously used format.
199-
*
200-
* @param {string} format - a format compliant with {@link String#format(String, Object...)} with 2 object arguments
201-
* @return {string} a previously used format
202-
* @throws {TypeError} if given format hasn't got two format specifiers <tt>"%s"</tt>, or if given format was
203-
* null
204-
*/
205-
Eid.setFormat = function(format) {
206-
validateFormat(format, FORMAT_NUM_SPEC);
207-
var previously = EidInternal.format;
208-
EidInternal.format = format;
209-
return previously;
210-
};
211-
212-
/**
213-
* Sets the actual format that will be used in {@link #toString()} method
214-
*
215-
* @param {string} refFormat - a format compliant with {@link String#format(String, Object...)} with 3 object arguments
216-
* @return {string} a previously used format
217-
* @throws {TypeError} if given format hasn't got tree format specifiers <tt>"%s"</tt>, or if given format was
218-
* null
219-
*/
220-
Eid.setRefFormat = function(refFormat) {
221-
validateFormat(refFormat, REF_FORMAT_NUM_SPEC);
222-
var previously = EidInternal.refFormat;
223-
EidInternal.refFormat = refFormat;
224-
return previously;
225-
};
226-
227-
/**
228-
* Sets a format that will be used for all Eid exceptions when printing a detail message.
229-
* <p>
230-
* Format must be non-null and contain two format specifiers <tt>"%s"</tt>
231-
*
232-
* @param {string} format - a format that will be used, must be non-null and contain two format specifiers <tt>"%s"</tt>
233-
* @return {string} previously used format
234-
* @throws {TypeError} if given format hasn't got two format specifiers <tt>"%s"</tt>, or if given format was
235-
* null
236-
*/
237-
Eid.setMessageFormat = function(format) {
238-
validateFormat(format, MESSAGE_FORMAT_NUM_SPEC);
239-
var oldFormat = EidInternal.messageFormat;
240-
EidInternal.messageFormat = format;
241-
return oldFormat;
242-
};
243-
244-
/**
245-
* Gets actually set message format
246-
*
247-
* @return {string} actually setted message format
248-
*/
249-
Eid.getMessageFormat = function() {
250-
return EidInternal.messageFormat;
251-
};
20+
var Eid = require('./eid/eid');
21+
Eid.preconditions = require('./eid/preconditions');
22+
Eid.exceptions = require('./eid/exceptions');
25223

25324
module.exports = Eid;
25425

0 commit comments

Comments
 (0)