IGNITE-9106 Web Console: Added bytes filter.

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/66d69c2b
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/66d69c2b
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/66d69c2b

Branch: refs/heads/ignite-8446
Commit: 66d69c2b328f200c4f8955f439b2c63dd8b6c415
Parents: 4d9f450
Author: Alexander Kalinin <verba...@yandex.ru>
Authored: Tue Jul 31 10:28:13 2018 +0700
Committer: Alexey Kuznetsov <akuznet...@apache.org>
Committed: Tue Jul 31 10:28:13 2018 +0700

----------------------------------------------------------------------
 modules/web-console/frontend/app/app.js         |  2 ++
 .../frontend/app/filters/bytes.filter.js        | 34 ++++++++++++++++++
 .../frontend/app/filters/bytes.filter.spec.js   | 36 ++++++++++++++++++++
 3 files changed, 72 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/66d69c2b/modules/web-console/frontend/app/app.js
----------------------------------------------------------------------
diff --git a/modules/web-console/frontend/app/app.js 
b/modules/web-console/frontend/app/app.js
index e807319..1f81f86 100644
--- a/modules/web-console/frontend/app/app.js
+++ b/modules/web-console/frontend/app/app.js
@@ -100,6 +100,7 @@ import AngularStrapSelect from 
'./services/AngularStrapSelect.decorator';
 
 // Filters.
 import byName from './filters/byName.filter';
+import bytes from './filters/bytes.filter';
 import defaultName from './filters/default-name.filter';
 import domainsValidation from './filters/domainsValidation.filter';
 import duration from './filters/duration.filter';
@@ -305,6 +306,7 @@ export default angular.module('ignite-console', [
 .service('Models', Models)
 // Filters.
 .filter('byName', byName)
+.filter('bytes', bytes)
 .filter('defaultName', defaultName)
 .filter('domainsValidation', domainsValidation)
 .filter('duration', duration)

http://git-wip-us.apache.org/repos/asf/ignite/blob/66d69c2b/modules/web-console/frontend/app/filters/bytes.filter.js
----------------------------------------------------------------------
diff --git a/modules/web-console/frontend/app/filters/bytes.filter.js 
b/modules/web-console/frontend/app/filters/bytes.filter.js
new file mode 100644
index 0000000..7576dad
--- /dev/null
+++ b/modules/web-console/frontend/app/filters/bytes.filter.js
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export default () => {
+    return (bytes, precision) => {
+        if (bytes === 0)
+            return '0 bytes';
+
+        if (isNaN(parseFloat(bytes)) || !isFinite(bytes))
+            return '-';
+
+        if (typeof precision === 'undefined')
+            precision = 1;
+
+        const units = ['bytes', 'kB', 'MB', 'GB', 'TB'];
+        const number = Math.floor(Math.log(bytes) / Math.log(1024));
+
+        return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) 
+ ' ' + units[number];
+    };
+};

http://git-wip-us.apache.org/repos/asf/ignite/blob/66d69c2b/modules/web-console/frontend/app/filters/bytes.filter.spec.js
----------------------------------------------------------------------
diff --git a/modules/web-console/frontend/app/filters/bytes.filter.spec.js 
b/modules/web-console/frontend/app/filters/bytes.filter.spec.js
new file mode 100644
index 0000000..884e43e
--- /dev/null
+++ b/modules/web-console/frontend/app/filters/bytes.filter.spec.js
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import bytesFilter from './bytes.filter';
+
+import { suite, test } from 'mocha';
+import { assert } from 'chai';
+
+const bytesFilterInstance = bytesFilter();
+
+suite('bytes filter', () => {
+    test('bytes filter', () => {
+        assert.equal(bytesFilterInstance(0), '0 bytes');
+        assert.equal(bytesFilterInstance(1000), '1000.0 bytes');
+        assert.equal(bytesFilterInstance(1024), '1.0 kB');
+        assert.equal(bytesFilterInstance(5000), '4.9 kB');
+        assert.equal(bytesFilterInstance(1048576), '1.0 MB');
+        assert.equal(bytesFilterInstance(104857600), '100.0 MB');
+        assert.equal(bytesFilterInstance(1073741824), '1.0 GB');
+        assert.equal(bytesFilterInstance(1099511627776), '1.0 TB');
+    });
+});

Reply via email to