For node
Install
npm install azheda-utils
Usage
const azhedautils = require('azheda-utils');
Sleep function
await azhedautils.sleep(1000);
This will stop the code for 1000ms (1 second)
Download function
Download a file from a url
await azhedautils.downloadFile(url, filename);
This function will download a file from a url and save it with a specific filename.
WebServer
const server = new azhedautils.WebServer({
host: 'localhost',
port: 7788
});
This function will start a webserver, the default command are limited.
MySurreal
const mysur = new azhedautils.MySurreal();
mysur.setUser('<your username>', '<your password>');
mysur.setNamespace('<namespace>');
mysur.setDatabase('<database>');
const mysur = new azhedautils.MySurreal({
username: '<your username>',
password: '<your password>',
namespace: '<namespace>',
database: '<database>',
host: '127.0.0.1',
port: 8000,
path: 'rpc'
});
await mysur.query('...');
await mysur.create('person:toby', { name: 'Toby', age: 20 });
await mysur.insert('person', [{ name: 'Toby', age: 20 }, { name: 'Derek', age: 30 }])
await mysur.patch('person:toby', { age: 25 });
await mysur.delete('person:toby');
await mysur.live('person');
await mysur.kill('<surreal query uuid>');
await mysur.close();
CheckTime
Once you've asign the new instance of the class to a variable, you just need to call .set() to save the current time
const chTime = new azhedautils.CheckTime();
(async () => {
chTime.set();
await azhedautils.sleep(4 * 1000)
console.log(chTime.set()+'ms');
>>> 4004ms
})();
Client
Installation:
<script src="https://unpkg.com/azheda-utils/dist/client.js"></script>
Class myStorage
const testValue = 123;
const testKey = 't-key';
const time = 1000 * 60 * 60;
Add value 123 to localStorage with 't-key' as key,
return true if the the value was set correctly, false or null otherwise
myStorage.add(testKey, testValue);
Checks if there's that key saved in localStorage and return the value,
if the key was not found, that returns null
myStorage.get(testKey);
If a value is saved in localStorage with that key, the value'll be removed,
return true if this was successful, false if there was some kind of problem
myStorage.remove(testKey);
Clear all data saved in localStorage,
returns 0 if everything was cleared correctly
myStorage.clear();
Same as myStorage.add(), but this time, the value has an expiry time.
myStorage.localcookie.set(testKey, testValue, time);
This function'll try to read and the value with the given key saved in localStorage.
if the key was not found this'll return null.
if the expiry time was passed, the value will'be cleared and this'll return null.
myStorage.localcookie.get(key);
Class Loader:
const theme = 'dark';
const loader = new Loader(theme);
loader.append();
loader.remove();
New properties (added with prototype):
Number.map
This will convert the number from the first range of numbers to the second one
const testNumber = 7;
console.log(testNumber.map(0, 10, 0, 100));
>>> 70
Number.getid
const myNumber = 123;
console.log(myNumber.getid());
>>> 3050889136
String.encodeAscii
const testString = 'Hello';
const asciiString = testString.encodeAscii();
console.log(asciiString);
>>> 72|101|108|108|111
String.decodeAscii
const testString = '72|101|108|108|111';
console.log(testString.decodeAscii());
>>> Hello
String.hashSeed
const testString = 'Hello';
console.log(testString.hashSeed());
>>> 4867314475583282
console.log(testString.hashSeed(1));
>>> 7386249923560166
console.log(testString.hashSeed(2));
>>> 715765420405280
String.getid
const myString = 'Hello';
console.log(myString.getid());
>>> 1987897324
String.capitalize
console.log('hello'.capitalize());
>>> Hello
console.log('Hello world'.capitalize());
>>> Hello world
Array.copy
Create a deepcopy of the array
const myList = [1, 2, 3];
console.log(myList.copy());
>>> [1, 2, 3]
Array.randomize
This function will return a copy of the array but every item has a new random index
const arr = [1, 2, 3, 4, 5];
console.log(arr.randomize());
>>> [5, 3, 1, 4, 2]
console.log(arr.randomize());
>>> [5, 1, 3, 2, 4]
console.log(arr.randomize());
>>> [1, 4, 5, 2, 3]
console.log(arr.randomize());
>>> [1, 3, 5, 2, 4]
Array.sortByList
This function will sort A-Z the array, and then put the given values at the start (in the same order)
const arrayToSort = [
'dog', 'cat', 'shark',
'parrot', 'giraffe',
'zebra', 'dolphin'
];
const newList = arrayToSort.sortByList(['zebra', 'dolphin']);
console.log(newList);
>>> ['zebra', 'dolphin', 'dog', 'cat', 'giraffe', 'parrot', 'shark']
Object.copy
Create a deepcopy of the object
const myObject = { x: 1, y: 2 };
console.log(myObject.copy());
>>> { x: 1, y: 2 }
Object.sort:
const obj = {
i: 1,
a: 2,
z: 3,
y: 4
};
console.log(obj.sort());
>>> { a: 2, i: 1 y: 4, z: 3 }
Object.getList
const obj = {
x: 1,
y: 2,
z: 3
};
console.log(obj.getList());
>>> [ ['x', 'y', 'z'], [ 1 , 2 , 3 ] ]
Dependencies