xrfragment-haxe/src/xrfragment/Url.hx

73 lines
4 KiB
Haxe
Raw Normal View History

2023-03-09 22:32:28 +01:00
package xrfragment;
2023-03-09 22:39:59 +01:00
@:expose // <- makes the class reachable from plain JavaScript
@:keep // <- avoids accidental removal by dead code elimination
2023-03-30 15:44:13 +02:00
// # URI Value types
//
// | type | info | format | example |
2023-03-30 15:57:58 +02:00
class Value { // |------|------|--------|----------------------------------|
2023-03-30 15:44:13 +02:00
public var x:Float; // |vector| x,y,z| comma-separated | #pos=1,2,3 |
public var y:Float; //
public var z:Float; //
public var color:String; // |string| color| FFFFFF (hex) | #fog=5m,FFAACC |
public var string:String; // |string| | | #q=-sun |
public var int:Int; // |int | | [-]x[xxxxx] | #price:>=100 |
public var float:Float; // |float | | [-]x[.xxxx] (ieee)| #prio=-20
public var args:Array<Value>; // |array | mixed| \|-separated | #pos=0,0,0|90,0,0 |
public function new(){} //
2023-03-30 16:23:47 +02:00
// > rule for thumb: type-limitations will piggyback JSON limitations (IEEE floatsize e.g.)
2023-03-10 18:49:16 +01:00
}
2023-03-09 22:32:28 +01:00
2023-03-10 18:49:16 +01:00
class Url {
2023-03-09 22:32:28 +01:00
2023-03-30 16:23:47 +02:00
@:keep // # Url parser (the gist of it)
public static function parse(qs:String):haxe.DynamicAccess<Dynamic> { //
var fragment:Array<String> = qs.split("#"); // 1. fragment URI starts with `#`
var splitArray:Array<String> = fragment[1].split('&'); // 1. fragments are split by `&`
var regexPlus = ~/\+/g; // 1. fragment-values are urlencoded (` ` becomes `+` and so on)
2023-03-17 16:06:24 +01:00
var resultMap:haxe.DynamicAccess<Dynamic> = {};
2023-03-09 22:32:28 +01:00
for (i in 0...splitArray.length) {
2023-03-30 16:23:47 +02:00
var splitByEqual = splitArray[i].split('='); // 1. `=` is used to indicate fragmentvalues
2023-03-10 18:49:16 +01:00
var key:String = splitByEqual[0];
var v:Value = new Value();
if (splitByEqual.length > 1) {
var value:String = StringTools.urlDecode(regexPlus.split(splitByEqual[1]).join(" "));
guessType(v, value);
2023-03-30 16:23:47 +02:00
// multiple/fallback values
if( value.split("|").length > 1 ){ // 1. `|` is used to indicate multiple/fallback values
2023-03-10 18:49:16 +01:00
v.args = new Array<Value>();
var args:Array<String> = value.split("|");
for( i in 0...args.length){
var x:Value = new Value();
guessType(x, args[i]);
v.args.push( x );
}
2023-03-09 22:32:28 +01:00
}
2023-03-10 18:49:16 +01:00
resultMap.set(key, v );
}
2023-03-09 22:32:28 +01:00
}
return resultMap;
}
2023-03-10 18:49:16 +01:00
@:keep
public static function guessType(v:Value, str:String):Void {
2023-03-30 16:23:47 +02:00
var isColor:EReg = ~/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/; // 1. hex colors are detected using regex `/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/`
var isInt:EReg = ~/^[0-9]+$/; // 1. integers are detected using regex `/^[0-9]+$/`
var isFloat:EReg = ~/^[0-9]+\.[0-9]+$/; // 1. floats are detected using regex `/^[0-9]+\.[0-9]+$/`
2023-03-10 18:49:16 +01:00
v.string = str;
2023-03-30 16:23:47 +02:00
if( str.split(",").length > 1){ // 1. `,` is used to detect vector 1D/2D/3D values like x[,y[,z]]
var xyz:Array<String> = str.split(","); // 1. anything else will be treated as string-value
if( xyz.length > 0 ) v.x = Std.parseFloat(xyz[0]); // 1. last resort: inappropriate string values will be converted using parseInt/parseFloat
2023-03-10 18:49:16 +01:00
if( xyz.length > 1 ) v.y = Std.parseFloat(xyz[1]);
if( xyz.length > 2 ) v.z = Std.parseFloat(xyz[2]);
}
if( isColor.match(str) ) v.color = str;
if( isFloat.match(str) ) v.float = Std.parseFloat(str);
if( isInt.match(str) ) v.int = Std.parseInt(str);
}
2023-03-09 22:32:28 +01:00
}