- 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
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
import { toHex } from "./string.js";
export * as agent from "./agent.js";
export * as array from "./array.js";
export * as file from "./file.js";
export * as string from "./string.js";
export * as function from "./function.js";
import { compare } from "semver";
/**
* a collection of utility functions
* @namespace utils
*/
// guid default value
let GUID_base = "";
let GUID_index = 0;
/**
* Compare two version strings
* @public
* @memberof utils
* @name checkVersion
* @param {string} v1 - First version string to compare
* @param {string} v2 - second version string to compare
* @returns {number} Return 0 if v1 == v2, or 1 if v1 is greater, or -1 if v2 is greater
* @example
* if (me.utils.checkVersion("7.0.0") > 0) {
* console.error(
* "melonJS is too old. Expected: 7.0.0, Got: 6.3.0"
* );
* }
*/
export function checkVersion(v1, v2) {
return compare(v1, v2);
}
/**
* parse the fragment (hash) from a URL and returns them into
* @public
* @memberof utils
* @name getUriFragment
* @param {string} [url=document.location] - an optional params string or URL containing fragment (hash) params to be parsed
* @returns {object} an object representing the deserialized params string.
* @property {boolean} [hitbox=false] draw the hitbox in the debug panel (if enabled)
* @property {boolean} [velocity=false] draw the entities velocity in the debug panel (if enabled)
* @property {boolean} [quadtree=false] draw the quadtree in the debug panel (if enabled)
* @property {boolean} [webgl=false] force the renderer to WebGL
* @property {boolean} [debug=false] display the debug panel (if preloaded)
* @property {string} [debugToggleKey="s"] show/hide the debug panel (if preloaded)
* @example
* // http://www.example.com/index.html#debug&hitbox=true&mytag=value
* let UriFragment = me.utils.getUriFragment();
* console.log(UriFragment["mytag"]); //> "value"
*/
export function getUriFragment(url) {
let hash = {};
if (typeof url === "undefined") {
if (typeof globalThis.document !== "undefined") {
let location = globalThis.document.location;
if (location && location.hash) {
url = location.hash;
} else {
// No "document.location" exist for Wechat mini game platform.
return hash;
}
} else {
// "document" undefined on node.js
return hash;
}
} else {
// never cache if a url is passed as parameter
let index = url.indexOf("#");
if (index !== -1) {
url = url.slice(index, url.length);
} else {
return hash;
}
}
// parse the url
url.slice(1).split("&").filter((value) => value !== "").forEach((value) => {
let kv = value.split("=");
let k = kv.shift();
let v = kv.join("=");
hash[k] = v || true;
});
return hash;
}
/**
* reset the GUID Base Name
* the idea here being to have a unique ID
* per level / object
* @ignore
*/
export function resetGUID(base, index = 0) {
// also ensure it's only 8bit ASCII characters
GUID_base = toHex(base.toString().toUpperCase());
GUID_index = index;
}
/**
* create and return a very simple GUID
* Game Unique ID
* @ignore
*/
export function createGUID(index = 1) {
// to cover the case of undefined id for groups
GUID_index += index;
return GUID_base + "-" + (index || GUID_index);
}