# 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
    100

    1
    20
    20
    20
    20
    3
    17
    6
    11
    1

    10

    1
    20
    2
    18
    1

    1
    20
    20
    1
    19
    5

    1
    20
    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);