all files / src/context/ constants.js

47.22% Statements 17/36
20% Branches 4/20
50% Functions 2/4
47.22% Lines 17/36
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148              1× 1×   1× 1× 1×           1×     1×                                                                                                                                               1×                                   1× 21× 21×       21×     21× 21×     21×           1×                               1×  
/*!
 * Copyright (C) 2017 Glayzzle (BSD3 License)
 * @authors https://github.com/glayzzle/php-transpiler/graphs/contributors
 * @url http://glayzzle.com
 */
'use strict';
 
var os = require('os');
var path = require('path');
 
var core = 'Core';
var user = 'user';
var std = 'standard';
 
/**
 * Handles constants
 * @constructor Constants
 */
var Constants = function() {
  // List of default constants
  // php -r "print_r(get_defined_constants(true));" > constants.txt
  this.items = {
    /** CORE CONSTANTS **/
    E_ERROR:             { value: 1, category: core },
    E_WARNING:           { value: 2, category: core },
    E_PARSE:             { value: 4, category: core },
    E_NOTICE:            { value: 8, category: core },
    E_CORE_ERROR:        { value: 16, category: core },
    E_CORE_WARNING:      { value: 32, category: core },
    E_COMPILE_ERROR:     { value: 64, category: core },
    E_COMPILE_WARNING:   { value: 128, category: core },
    E_USER_ERROR:        { value: 256, category: core },
    E_USER_WARNING:      { value: 512, category: core },
    E_USER_NOTICE:       { value: 1024, category: core },
    E_STRICT:            { value: 2048, category: core },
    E_RECOVERABLE_ERROR: { value: 4096, category: core },
    E_DEPRECATED:        { value: 8192, category: core },
    E_USER_DEPRECATED:   { value: 16384, category: core },
    E_ALL:               { value: 32767, category: core },
    PHP_OS:              { value: os.type(), category: core },
    PHP_EOL:             { value: os.EOL, category: core },
    /** STANDARD **/
    CONNECTION_ABORTED:  { value: 1, category: std },
    CONNECTION_NORMAL:   { value: 0, category: std },
    CONNECTION_TIMEOUT:  { value: 2, category: std },
    INI_USER:            { value: 1, category: std },
    INI_PERDIR:          { value: 2, category: std },
    INI_SYSTEM:          { value: 4, category: std },
    INI_ALL:             { value: 7, category: std },
    INI_SCANNER_NORMAL:  { value: 0, category: std },
    INI_SCANNER_RAW:     { value: 1, category: std },
    PHP_URL_SCHEME:      { value: 0, category: std },
    PHP_URL_HOST:        { value: 1, category: std },
    PHP_URL_PORT:        { value: 2, category: std },
    PHP_URL_USER:        { value: 3, category: std },
    PHP_URL_PASS:        { value: 4, category: std },
    PHP_URL_PATH:        { value: 5, category: std },
    PHP_URL_QUERY:       { value: 6, category: std },
    PHP_URL_FRAGMENT:    { value: 7, category: std },
    PHP_QUERY_RFC1738:   { value: 1, category: std },
    PHP_QUERY_RFC3986:   { value: 2, category: std },
    M_E:                 { value: 2.718281828459, category: std },
    M_LOG2E:             { value: 1.442695040889, category: std },
    M_LOG10E:            { value: 0.43429448190325, category: std },
    M_LN2:               { value: 0.69314718055995, category: std },
    M_LN10:              { value: 2.302585092994, category: std },
    M_PI:                { value: 3.1415926535898, category: std },
    M_PI_2:              { value: 1.5707963267949, category: std },
    M_PI_4:              { value: 0.78539816339745, category: std },
    M_1_PI:              { value: 0.31830988618379, category: std },
    M_2_PI:              { value: 0.63661977236758, category: std },
    M_SQRTPI:            { value: 1.7724538509055, category: std },
    M_2_SQRTPI:          { value: 1.1283791670955, category: std },
    M_LNPI:              { value: 1.1447298858494, category: std },
    M_EULER:             { value: 0.57721566490153, category: std },
    M_SQRT2:             { value: 1.4142135623731, category: std },
    M_SQRT1_2:           { value: 0.70710678118655, category: std },
    M_SQRT3:             { value: 1.7320508075689, category: std },
    INF:                 { value: 'INF', category: std },
    NAN:                 { value: 'NAN', category: std },
    PHP_ROUND_HALF_UP:   { value: 1, category: std },
    PHP_ROUND_HALF_DOWN: { value: 2, category: std },
    PHP_ROUND_HALF_EVEN: { value: 3, category: std },
    PHP_ROUND_HALF_ODD:  { value: 4, category: std },
    /* ... */
    DIRECTORY_SEPARATOR: { value: path.sep, category: std },
    PATH_SEPARATOR:      { value: path.delimiter, category: std },
  };
};
 
/**
 * Sets a constant value
 */
Constants.prototype.set = function(name, contents, insensitive) {
  if (insensitive) {
    name = name.toUpperCase();
  }
  if (!(name in this.items)) {
    this.items[name] = {
      value: contents,
      category: user,
      insensitive: insensitive
    };
    return true;
  }
  return false;
};
 
/**
 * Reads a constant value
 */
Constants.prototype.get = function(name) {
  var shouldInsensitive = false;
  Iif (!(name in this.items)) {
    name = name.toUpperCase();
    shouldInsensitive = true;
  }
  Iif (!(name in this.items)) {
    return undefined;
  }
  var constant = this.items[name];
  Iif (shouldInsensitive && !constant.insensitive) {
    return undefined;
  }
  return constant.value;
};
 
/**
 * Check if the specified constant exists
 */
Constants.prototype.has = function(name) {
  var shouldInsensitive = false;
  if (!(name in this.items)) {
    name = name.toUpperCase();
    shouldInsensitive = true;
  }
  if (!(name in this.items)) {
    return false;
  }
  if (shouldInsensitive && !this.items[name].insensitive) {
    return false;
  }
  return true;
};
 
 
module.exports = Constants;