Issues (204)

src/EnumBuilder.js (3 issues)

Severity
1
/* Javascript Object Inheritance Implementation                ______  ________
2
 * (c) 2016 <[email protected]>                             __ / / __ \/  _/  _/
3
 * Licensed under MIT.                                    / // / /_/ // /_/ /
4
 * ------------------------------------------------------ \___/\____/___/__*/
5
6
JOII = typeof (JOII) !== 'undefined' ? JOII : {};
7
JOII.EnumRegistry = {};
8
9
/**
10
 * An enumerator can be used for type checking to validate if the given
11
 * value exists within the object as a constant value.
12
 */
13
JOII.EnumBuilder = JOII.ClassBuilder({ 'final' : true }, {
14
15
    'public immutable string name'      : null,
16
    'public immutable object constants' : {},
17
18
    /**
19
     * @param {String} name
20
     * @param {Object} obj
21
     */
22
    __construct: function(name, obj)
23
    {
24
        this.name      = name;
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
25
        this.constants = obj;
26
    },
27
28
    /**
29
     * Returns true if a constant with the given value exists within this
30
     * enumerator.
31
     *
32
     * @param  {*} value
33
     * @return bool
34
     */
35
    contains: function(value)
36
    {
37
        for (var i in this.constants) {
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
38
            if (this.constants.hasOwnProperty(i) === false) continue;
39
            if (this.constants[i] === value) {
40
                return true;
41
            }
42
        }
43
        return false;
44
    },
45
46
    /**
47
     * Registers a new Enumerator type with the given name and object.
48
     *
49
     * @param  {String} name
50
     * @param  {Object} obj
51
     * @return JOII.EnumBuilder
52
     */
53
    __call: function(name, obj) {
54
        if (typeof (name) !== 'string') {
0 ignored issues
show
This function should enable strict mode with use strict.

Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.

Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.

We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.

Loading history...
55
            throw 'Argument #1 of Enum must be a string, ' + typeof (name) + ' given.';
56
        }
57
        if (typeof (obj) === 'function' &&
58
            typeof (obj.prototype.__joii__) !== 'undefined') {
59
            obj = obj.prototype.__joii__.constants;
60
        }
61
62
        if (typeof (obj) !== 'object') {
63
            throw 'Argument #2 of Enum must be an object or definition, ' + typeof (obj) + ' given.';
64
        }
65
66
        if (typeof (JOII.EnumRegistry[name.toLowerCase()]) !== 'undefined') {
67
            throw 'Enumerator "' + name + '" already exists.';
68
        }
69
70
        var enumerator = new JOII.EnumBuilder(name, obj);
71
        for (var i in obj) {
72
            if (typeof (obj[i]) === 'function') {
73
                throw 'An enumerator cannot contain functions. "' + i + '" is a function.';
74
            }
75
            if (typeof (obj[i]) === 'object') {
76
                throw 'An enumerator cannot contain objects. "' + i + '" is an object.';
77
            }
78
            JOII.CreateProperty(enumerator, i, obj[i], false);
79
        }
80
        JOII.EnumRegistry[name.toLowerCase()] = enumerator;
81
        return enumerator;
82
    }
83
});
84