Skip to content

Instantly share code, notes, and snippets.

@lasellers
Created April 23, 2017 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lasellers/a5bbbae8ad96c7f0956b5ee44dbe40f9 to your computer and use it in GitHub Desktop.
Save lasellers/a5bbbae8ad96c7f0956b5ee44dbe40f9 to your computer and use it in GitHub Desktop.
Sort2 (e) promise+fetch
<!DOCTYPE html>
<html>
<body>
<p id="stdout"></p>
<script>
// @author lasellers@gmail.com Lewis A. Sellers
// sort2e with class literals (ie, object literals as classes) and es6 fetch
var Units = {
unitsFilename: 'units.txt',
noBlankLinesFilter: function (a) {
return a.length !== 0;
},
loadData: function (callback) {
return new Promise(function (resolve, reject) {
fetch(Units.unitsFilename, {
method: 'get'
}).then(function (response) {
return response.text();
}).then(function (text) {
resolve(Units.rawTextToObjectArray(text));
}).catch(function (err) {
reject(err);
});
});
},
rawTextToObjectArray: function (raw) {
var lines = raw.trim().split('\n');
var unsorted = lines.filter(Units.noBlankLinesFilter).map(function (raw) {
try {
var parts = raw.split("-");
// strip non-alphanumerics
var id = parts[0].replace(/[^A-Za-z0-9]/g, '');
// group matches by alpha and numeric so we split for ex: 17b into "17" and "b"
var items = id.match(/([0-9]+|[a-zA-Z]+)/g);
var unit, subunit;
// create objects with unit, subunit, raw fields to sort on
switch (items.length) {
case 1:
unit = items[0];
return {'unit': unit, 'subunit': '', 'raw': raw};
case 2:
unit = items[0];
subunit = items[1];
return {'unit': unit, 'subunit': subunit, 'raw': raw};
}
} catch (e) {
// ignores lines with malformed data
}
});
return unsorted;
},
// sorts by unit as number
// if units same (ie, 17 - 17 = 0) then returns second expression: a string compare of subunits
// otherwise returns the unit compare (first expression)
sortByUnitNumber: function (unsorted) {
return new Promise(function (resolve, reject) {
unsorted.sort(function (a, b) {
return parseInt(a["unit"]) - parseInt(b["unit"]) || a["subunit"].localeCompare(b["subunit"]);
});
resolve(unsorted);
});
},
saveData: function (sorted) {
sorted.forEach(function (item) {
window.log(item.raw);
});
}
}
;
window.log = function (message) {
console.log(JSON.stringify(message));
var el = document.getElementById("stdout");
el.innerHTML += message + '<br>';
}
function docReady(callback) {
if (document.readyState != 'loading') {
callback();
} else {
document.addEventListener('DOMContentLoaded', callback);
}
}
docReady(function () {
console.log("docReady");
Units.loadData()
.then(function (unsorted) {
Units.sortByUnitNumber(unsorted)
.then(function (sorted) {
Units.saveData(sorted);
})
.catch(function (err) {
console.error("Sort error: " + err.statusText);
});
})
.catch(function (err) {
console.error("Load error: " + err.statusText);
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment