summaryrefslogtreecommitdiffstats
path: root/lib/api/deprecate.js
blob: b8907774575c6bc63bf50e9adae3b42deb64d282 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var logged = {};

/**
    Log a deprecated notice

    @param {Book|Output} book
    @param {String} key
    @param {String} message
*/
function logNotice(book, key, message) {
    if (logged[key]) return;

    logged[key] = true;

    var logger = book.getLogger();
    logger.warn.ln(message);
}

/**
    Deprecate a function

    @param {Book|Output} book
    @param {String} key: unique identitifer for the deprecated
    @param {Function} fn
    @param {String} msg: message to print when called
    @return {Function}
*/
function deprecateMethod(book, key, fn, msg) {
    return function() {
        logNotice(book, key, msg);

        return fn.apply(this, arguments);
    };
}

/**
    Deprecate a property of an object

    @param {Book|Output} book
    @param {String} key: unique identitifer for the deprecated
    @param {Object} instance
    @param {String} property
    @param {String} msg: message to print when called
    @return {Function}
*/
function deprecateField(book, key, instance, property, value, msg) {
    var getter = function(){
        logNotice(book, key, msg);
        return value;
    };
    var setter = function(v) {
        logNotice(book, key, msg);
        value = v;
        return value;
    };

    Object.defineProperty(instance, property, {
        get: getter,
        set: setter,
        enumerable: true
    });
}

module.exports = {
    method: deprecateMethod,
    field: deprecateField
};