-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyNumber.psm1
101 lines (88 loc) · 2.77 KB
/
MyNumber.psm1
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
#region Main
#define class
Class MyNumber {
#properties
[double]$Number
[double]$Square
[double]$Cube
[double]$Sqrt
[double]$Log
[double]$Sine
[double]$Cosine
[double]$Tangent
[double]$CircleArea
[double]$Inverse
[boolean]$IsEven
[boolean]$IsPrime
[double]$Exp
[double]$Factorial
[double[]]$Factors
[PSObject]$Custom
#store the custom scriptblock in a hidden property which can be managed through a module function
hidden [scriptblock]$CustomScriptBlock
#methods
[MyNumber] Refresh() {
$this.Square = ($this.number * $this.number)
$this.Cube = [math]::Pow($this.number, 3)
$this.Sqrt = [math]::Sqrt($this.number)
$this.Log = [math]::Log($this.number)
$this.Sine = [math]::Sin($this.number)
$this.Cosine = [math]::Cos($this.number)
$this.Tangent = [math]::Tan($this.number)
$this.CircleArea = [math]::PI * ($this.number * $this.number)
$this.Inverse = 1 / $this.number
$this.Exp = [math]::Exp($this.number)
$this.Factorial = (1..$this.number | ForEach-Object -Begin { $r = 1 } -Process { $r *= $_ } -End { $r })
$this.Factors = (1..$($this.number) | Where-Object { -Not ($this.number % $_) })
$this.IsEven = $this.TestIsEven()
$this.IsPrime = $this.TestIsPrime()
if ($this.CustomScriptBlock) {
$CustomResult = Invoke-Command -ScriptBlock $this.CustomScriptBlock -ArgumentList $this.Number
}
else {
$CustomResult = 0
}
$this.Custom = $CustomResult
#class methods require the Return keyword
Return $this
}
[boolean]TestIsPrime() {
if ($this.factors.count -eq 2) {
return $True
}
else {
return $False
}
}
[boolean]TestIsEven() {
if ($this.number % 2 -eq 0) {
Return $True
}
else {
Return $False
}
}
[String]ToBinary() {
$r = [convert]::ToString($this.Number, 2)
Return $r
}
[String]ToOctal() {
$r = [convert]::ToString($this.Number, 8)
Return $r
}
[String]ToHex() {
$r = [convert]::ToString($this.Number, 16)
Return $r
}
#constructor
MyNumber([double]$Number) {
$this.Number = $Number
$this.Refresh()
}
}
#extend the class with custom type extensions
Update-TypeData -TypeName MyNumber -MemberType ScriptProperty -MemberName Computername -Value {[System.Environment]::MachineName} -Force
Update-TypeData -TypeName MyNumber -MemberType AliasProperty -MemberName Value -Value Number -Force
#endregion
#dot source module functions
Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 | ForEach-Object -Process { . $_.FullName }