1+ function Get-Vector
2+ {
3+ <#
4+ . SYNOPSIS
5+ Gets a one dimensional vector
6+ . DESCRIPTION
7+ Gets a one dimensional vector (or, more simply, a list of numbers)
8+
9+ This will convert a variety of types into numbers.
10+ . NOTES
11+ This attempts to convert any type into a number.
12+
13+ Some types are special:
14+
15+ * Primitive types will be casted to float
16+ * `[Numerics.Vector2]`,`[Numerics.Vector3]`,`[Numerics.Vector4]` output each component
17+ * `[string]`s that match a range (`$start..$end`) will output that range
18+ * `[Version]`s will output each numeric component
19+ * `[semver]`s will output each numeric component, followed by the bytes of a release type
20+ * `[DateTime]` and `[DateTimeOffset]` will become a series of 12 numbers
21+ * `year`,`month`,`day`
22+ * `hour`, `minute`, `second`
23+ * `millisecond`, `microsecond`, `nanosecond`
24+ * `offset.hours`, `offset.minutes`, `offset.seconds`
25+ * `[string]s` will return their bytes in the current `$outputEncoding`
26+ * Anything unknown will be stringified and the bytes will be returned
27+ #>
28+ [Alias (' Vector' , ' Vector1' , ' V1' )]
29+ param ()
30+
31+ filter toVector {
32+ $arg = $_
33+ # Return primitive types
34+ if ($arg.GetType -and $arg.GetType ().IsPrimitive) {
35+ # casted to float
36+ return ($arg -as [float ])
37+ }
38+ # Return vector components
39+ if ($arg -is [ValueType ]) {
40+ if ($arg -is [Numerics.Vector2 ]) {
41+ return $arg.X , $arg.Y
42+ }
43+ elseif ($arg -is [Numerics.Vector3 ]) {
44+ return $arg.X , $arg.Y , $arg.Z
45+ }
46+ elseif ($arg -is [Numerics.Vector4 ]) {
47+ return $arg.X , $arg.Y , $arg.Z , $arg.W
48+ }
49+ }
50+ # Look for inline ranges.
51+ if ($arg -is [string ]) {
52+ if ($arg -match ' ^\d..\d' ) {
53+ $start , $end = $arg -split ' \..' , 2
54+ $startInt = ($start -as [int ])
55+ $endInt = ($end -as [int ])
56+ if ($null -ne $startInt -and $null -ne $endInt ) {
57+ # If found, return them expanded.
58+ return ($startInt .. $endInt )
59+ }
60+ }
61+ if ($arg -as [float ]) {
62+ return $arg -as [float ]
63+ }
64+ }
65+
66+
67+ # If the arg is a version, get each number of the version
68+ if ($arg -is [version ]) {return $arg.Major , $arg.Minor , $arg.Build , $arg.Revision }
69+
70+ # If we support semver and the arg is semver
71+ if ((' semver' -as [type ]) -and $arg -is [semver ]) {
72+ # Return the numeric parts of the semver
73+ $arg.Major , $arg.Minor , $arg.Patch
74+ # and turn any string portions to bytes
75+ if ($arg.PreReleaseLabel ) {
76+ # make sure to include a leading dash for pre-releases
77+ $OutputEncoding.GetBytes (" -$ ( $arg.PreReleaseLabel ) " )
78+ }
79+
80+ if ($arg.BuildLabel ) {
81+ # make sure to include a leading plus for build labels
82+ $OutputEncoding.GetBytes (" +$ ( $arg.BuildLabel ) " )
83+ }
84+ return
85+ }
86+
87+ # If the arg is a datetime or datetimeoffset
88+ if ($arg -is [DateTime ] -or $arg -is [DateTimeOffset ]) {
89+ # make it an offset, and then output 12 values
90+ $dateArg = $arg -as [DateTimeOffset ]
91+ # * `year` `month` `day`
92+ $dateArg.Year , $dateArg.Month , $dateArg.Day ,
93+ # * `hour` `minute` `second`
94+ $dateArg.Hour , $dateArg.Minute , $dateArg.Second ,
95+ # * `millisecond`, `microsecond`, `nanosecond`
96+ $dateArg.Millisecond , $dateArg.Microsecond , $dateArg.Nanosecond ,
97+ # * `offset hours`, `offset minutes`, `offset seconds`
98+ $dateArg.Offset.Hours , $dateArg.Offset.Minutes , $dateArg.Offset.Seconds
99+ return
100+ }
101+ # If the arg is a string
102+ if ($arg -is [string ]) {
103+ # return its bytes
104+ return $OutputEncoding.GetBytes ($arg )
105+ }
106+ # any input we have not caught, stringify and turn to bytes
107+ return $OutputEncoding.GetBytes (" $arg " )
108+ }
109+
110+
111+ # Collect all of our input and arguments
112+ $allIn = @ ($input ) + @ (
113+ foreach ($arg in $args ) {
114+ $arg
115+ }
116+ )
117+
118+ return $allIn | toVector
119+ }
0 commit comments