# all files / Core/Services/ StringToLiteralService.js

100% Statements 30/30
100% Branches 19/19
100% Functions 5/5
100% Lines 30/30

    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

    1

    1

    1
    101

    1
    21
    21
    21
    21
    3
    18
    6
    12
    1

    11

    1
    21
    2
    19
    1

    1
    21
    21
    1
    20
    5

    1
    21
    1

    1
     'use strict';

    // Utilities:
    var _ = require('lodash');

    // Module:
    var Core = require('../Core');

    var StringToLiteralService = function () {
        return {
            toLiteral: toLiteral
        };

        function toLiteral (value) {
            var boolean = toBoolean(value);
            var number = toNumber(value);
            var nil = toNull(value);
            if (boolean != null) {
                return boolean;
            } else if (number != null) {
                return number;
            } else if (nil === null) {
                return nil;
            } else {
                return value;
            }
        }

        function toBoolean (value) {
            if (value === 'true') {
                return true;
            } else if (value === 'false') {
                return false;
            }
        }

        function toNumber (value) {
            var number = +value;
            if (value === 'NaN') {
                return NaN;
            } else if (value && _.isNumber(number) && !_.isNaN(number)) {
                return number;
            }
        }

        function toNull (value) {
            if (value === 'null') {
                return null;
            }
        }
    };

    Core.service('stringToLiteralService', StringToLiteralService);