Module: DataTypeUtil

Defined in:
lib/wingify/utils/data_type_util.rb

Overview

Copyright 2024-2026 Wingify Software Pvt. Ltd.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Class Method Summary collapse

Class Method Details

.get_type(val) ⇒ Object

Determines the type of the given value



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
# File 'lib/wingify/utils/data_type_util.rb', line 77

def self.get_type(val)
  case
  when is_object(val)
    "Object"
  when is_array(val)
    "Array"
  when is_null(val)
    "Null"
  when is_undefined(val)
    "Undefined"
  when is_nan(val)
    "NaN"
  when is_number(val)
    "Number"
  when is_string(val)
    "String"
  when is_boolean(val)
    "Boolean"
  when is_date(val)
    "Date"
  when is_regex(val)
    "Regex"
  when is_function(val)
    "Function"
  else
    "Unknown Type"
  end
end

.is_array(val) ⇒ Object

Checks if the value is an Array



22
23
24
# File 'lib/wingify/utils/data_type_util.rb', line 22

def self.is_array(val)
  val.is_a?(Array)
end

.is_boolean(val) ⇒ Object

Checks if the value is a Boolean



52
53
54
# File 'lib/wingify/utils/data_type_util.rb', line 52

def self.is_boolean(val)
  val.is_a?(TrueClass) || val.is_a?(FalseClass)
end

.is_date(val) ⇒ Object

Checks if the value is a Date



62
63
64
# File 'lib/wingify/utils/data_type_util.rb', line 62

def self.is_date(val)
  val.is_a?(Date) || val.is_a?(Time) || val.is_a?(DateTime)
end

.is_defined(val) ⇒ Object

Checks if the value is defined (not nil)



37
38
39
# File 'lib/wingify/utils/data_type_util.rb', line 37

def self.is_defined(val)
  !val.nil?
end

.is_function(val) ⇒ Object

Checks if the value is a Function (Proc or Lambda in Ruby)



67
68
69
# File 'lib/wingify/utils/data_type_util.rb', line 67

def self.is_function(val)
  val.is_a?(Proc) || val.is_a?(Method)
end

.is_nan(val) ⇒ Object

Checks if the value is NaN (only applicable for Float in Ruby)



57
58
59
# File 'lib/wingify/utils/data_type_util.rb', line 57

def self.is_nan(val)
  val.is_a?(Float) && val.nan?
end

.is_null(val) ⇒ Object

Checks if the value is nil



27
28
29
# File 'lib/wingify/utils/data_type_util.rb', line 27

def self.is_null(val)
  val.nil?
end

.is_number(val) ⇒ Object

Checks if the value is a Number (including NaN)



42
43
44
# File 'lib/wingify/utils/data_type_util.rb', line 42

def self.is_number(val)
  val.is_a?(Numeric)
end

.is_object(val) ⇒ Object

Checks if the value is a Hash (object in JS)



17
18
19
# File 'lib/wingify/utils/data_type_util.rb', line 17

def self.is_object(val)
  val.is_a?(Hash)
end

.is_regex(val) ⇒ Object

Checks if the value is a Regular Expression



72
73
74
# File 'lib/wingify/utils/data_type_util.rb', line 72

def self.is_regex(val)
  val.is_a?(Regexp)
end

.is_string(val) ⇒ Object

Checks if the value is a String



47
48
49
# File 'lib/wingify/utils/data_type_util.rb', line 47

def self.is_string(val)
  val.is_a?(String)
end

.is_undefined(val) ⇒ Object

Checks if the value is undefined (not applicable in Ruby, so return false)



32
33
34
# File 'lib/wingify/utils/data_type_util.rb', line 32

def self.is_undefined(val)
  false
end