Yuki Takei 6 lat temu
rodzic
commit
00ed729645

+ 1 - 0
packages/growi-commons/src/index.js

@@ -1,6 +1,7 @@
 module.exports = {
   BasicInterceptor: require('./util/basic-interceptor'),
   pathUtils: require('./util/path-utils'),
+  envUtils: require('./util/env-utils'),
   // plugin
   customTagUtils: require('./plugin/util/custom-tag-utils'),
   // service

+ 23 - 0
packages/growi-commons/src/test/util/env-utils.test.js

@@ -0,0 +1,23 @@
+require('module-alias/register');
+
+const envUtils = require('@src/util/env-utils');
+
+
+describe('env-utils', () => {
+  describe('.toBoolean', () => {
+
+    test('should convert to true', () => {
+      expect(envUtils.toBoolean('true')).toBe(true);
+      expect(envUtils.toBoolean('True')).toBe(true);
+      expect(envUtils.toBoolean(1)).toBe(true);
+    });
+
+    test('should convert to false', () => {
+      expect(envUtils.toBoolean(undefined)).toBe(false);
+      expect(envUtils.toBoolean(null)).toBe(false);
+      expect(envUtils.toBoolean('false')).toBe(false);
+      expect(envUtils.toBoolean(0)).toBe(false);
+    });
+
+  });
+});

+ 17 - 0
packages/growi-commons/src/util/env-utils.js

@@ -0,0 +1,17 @@
+/**
+ * convert to boolean
+ *
+ * @param {string} value
+ * @returns {boolean}
+ * @memberof envUtils
+ */
+function toBoolean(value) {
+  return /^(true|1)$/i.test(value);
+}
+
+/**
+ * @namespace envUtils
+ */
+module.exports = {
+  toBoolean,
+};