Skip to content

Instantly share code, notes, and snippets.

View andrezimmermann's full-sized avatar

André Zimmermann andrezimmermann

View GitHub Profile
import play.api.libs.json._
import play.api.libs.functional.syntax._
import play.api.data.validation.ValidationError
implicit class JsPathPimps(path: JsPath) extends JsPath {
def readOrError[T](error: => String)(implicit r: Reads[T]): Reads[T] = new Reads[T] {
def reads(json: JsValue): JsResult[T] = path.readNullable(r).reads(json) match {
case JsSuccess(Some(value), _) => JsSuccess(value, path)
case JsSuccess(None, _) => JsError((path, ValidationError(error)))
case err@JsError(_) => err
@thomseddon
thomseddon / gist:3511330
Last active March 8, 2023 03:39
AngularJS byte format filter
app.filter('bytes', function() {
return function(bytes, precision) {
if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
if (typeof precision === 'undefined') precision = 1;
var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
number = Math.floor(Math.log(bytes) / Math.log(1024));
return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
}
});