Skip to content

Instantly share code, notes, and snippets.

@max-pub
max-pub / XML.js
Last active September 3, 2019 11:43
XML = {
parse: (string, type = 'text/xml') => new DOMParser().parseFromString(string, type), // like JSON.parse
stringify: DOM => new XMLSerializer().serializeToString(DOM), // like JSON.stringify
transform: (xml, xsl) => {
let proc = new XSLTProcessor();
proc.importStylesheet(typeof xsl == 'string' ? XML.parse(xsl) : xsl);
let output = proc.transformToFragment(typeof xml == 'string' ? XML.parse(xml) : xml, document);
return typeof xml == 'string' ? XML.stringify(output) : output; // if source was string then stringify response, else return object
},
mapSearch = (query) => {
return new Promise((resolve, reject) => {
fetch("https://maps.google.com/maps/api/geocode/json?sensor=true&address=" + query)
.then((resp) => resp.json())
.then(function(data) {
let types = {
country: 'country',
administrative_area_level_1: 'state',
administrative_area_level_2: 'county',
locality: 'city',
@max-pub
max-pub / post.php
Last active November 25, 2017 10:38
PHP POST
<?
function post($url, $data){
$options = array(
'http' => array( // use key 'http' even if you send the request to https://...
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
// 'content' => json_encode($data),
),
@max-pub
max-pub / uniqueID.js
Last active January 11, 2021 00:25
generate a unique ID
uniqueID = (length=6, n=true, lc=true, uc=true) => { // 62^6 = 50 Billion
let chars = '';
if (n) chars += '1234567890';
if (lc) chars += 'abcdefghijklmnopqrstuvwxyz';
if (uc) chars += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let ID = '';
for (let i = 0; i < length; i++)
ID += chars[Math.floor((Math.random() * chars.length))];
return ID;
}