2023-03-09 22:32:28 +01:00
|
|
|
package xrfragment;
|
|
|
|
|
|
2023-03-31 12:58:53 +02:00
|
|
|
import xrfragment.Parser;
|
|
|
|
|
|
2023-03-30 19:51:32 +02:00
|
|
|
@:expose // <- makes the class reachable from plain JavaScript
|
|
|
|
|
@:keep // <- avoids accidental removal by dead code elimination
|
2023-03-31 14:47:54 +02:00
|
|
|
//
|
2023-03-31 15:57:35 +02:00
|
|
|
// ### example URI: `://domain.com/some3d.asset#pos=1,0,0&prio=-5`
|
2023-03-31 14:47:54 +02:00
|
|
|
class URI {
|
2023-03-31 14:40:24 +02:00
|
|
|
@:keep // # URI parser
|
2023-03-31 12:58:53 +02:00
|
|
|
public static function parse(qs:String):haxe.DynamicAccess<Dynamic> { //
|
2023-03-31 14:47:54 +02:00
|
|
|
var fragment:Array<String> = qs.split("#"); // > icanhazcode? yes, see [URI.hx](./../src/xrfragment/URI.hx)
|
2023-03-31 14:40:24 +02:00
|
|
|
var splitArray:Array<String> = fragment[1].split('&'); //
|
|
|
|
|
var resultMap:haxe.DynamicAccess<Dynamic> = {}; // 1. fragment URI starts with `#`
|
|
|
|
|
for (i in 0...splitArray.length) { // 1. fragments are split by `&`
|
|
|
|
|
|
2023-03-30 19:51:32 +02:00
|
|
|
var splitByEqual = splitArray[i].split('='); // 1. `=` is used to split fragment key/values
|
2023-03-31 12:58:53 +02:00
|
|
|
var regexPlus = ~/\+/g; // 1. fragment-values are urlencoded (space becomes `+` using `encodeUriComponent` e.g.)
|
2023-03-30 19:51:32 +02:00
|
|
|
var key:String = splitByEqual[0];
|
2023-03-10 18:49:16 +01:00
|
|
|
|
2023-03-30 19:51:32 +02:00
|
|
|
if (splitByEqual.length > 1) {
|
|
|
|
|
var value:String = StringTools.urlDecode(regexPlus.split(splitByEqual[1]).join(" "));
|
2023-03-31 12:58:53 +02:00
|
|
|
var ok:Bool = Parser.parse(key,value,resultMap); // 1. every recognized fragment key/value-pair is added to a central map/associative array/object
|
2023-03-09 22:32:28 +01:00
|
|
|
}
|
2023-03-30 19:51:32 +02:00
|
|
|
}
|
|
|
|
|
return resultMap;
|
2023-03-09 22:32:28 +01:00
|
|
|
}
|
|
|
|
|
}
|