UNPKG

@carboncollins/mobileconfig

Version:

Create and sign iOS mobileconfig configuration files

145 lines (125 loc) 4.55 kB
/* eslint no-param-reassign: ["error", { "ignorePropertyModificationsFor": ["value"] }] */ 'use strict'; /** * @description Determins if an input `value` is empty/null/undefined/0-length/no-keys * @author Steven Collins <steven@carboncollins.uk> * @export * @param {any} value an input value to be checked * @returns {boolean} */ Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.isValueEmpty = isValueEmpty; exports.deleteEmptyKeys = deleteEmptyKeys; exports.toSafeString = toSafeString; exports.toSafeInteger = toSafeInteger; exports.toSafeFloat = toSafeFloat; exports.toSafeBoolean = toSafeBoolean; exports.toSafeArray = toSafeArray; exports.toSafeObject = toSafeObject; exports.toSafeDate = toSafeDate; exports.toSafeData = toSafeData; exports.hasRequiredValues = hasRequiredValues; function isValueEmpty(value) { return value === undefined || value === null || Array.isArray(value) && value.length === 0 || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === _typeof({}) && Object.keys(value).length === 0; } /** * @description removes any keys in an object that are consiered empty * @author Steven Collins <steven@carboncollins.uk> * @export * @param {Object} value * @returns {Object} returns the existing object reference but with the empty keys deleted */ function deleteEmptyKeys(value) { Object.keys(value).forEach(function (key) { return isValueEmpty(value[key]) && delete value[key]; }); return value; } /** * @description formats the input value into a string or null value if it is unable too * @author Steven Collins <steven@carboncollins.uk> * @export * @param {any} value the input value to convert to a safe string * @returns {?String} */ function toSafeString(value) { return value === undefined || value === null ? null : String(value); } /** * @description formats the input value into a integer or null value if it is unable too * @author Steven Collins <steven@carboncollins.uk> * @export * @param {any} value the input value to convert to a safe integer * @returns {?Number} */ function toSafeInteger(value) { if (value === undefined || value === null) { return null; } var intTest = Number.parseInt(value, 10); return Number.isInteger(intTest) ? intTest : null; } /** * @description formats the input value into a float or null value if it is unable too * @author Steven Collins <steven@carboncollins.uk> * @export * @param {any} value the input value to convert to a safe float * @returns {?Number} */ function toSafeFloat(value) { if (value === undefined || value === null) { return null; } var floatTest = Number.parseFloat(value, 10); return !Number.isNaN(floatTest) ? floatTest : null; } function toSafeBoolean(value) { if (value === undefined || value === null || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== _typeof(true)) { return null; } else { return Boolean(value); } }; function toSafeArray(value) { if (value === undefined || value === null || !Array.isArray(value)) { return null; } else { return value.filter(function (item) { return !isValueEmpty(item); }); } }; function toSafeObject(value) { if (value === undefined || value === null || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== _typeof({})) { return null; } else { return deleteEmptyKeys(value); // need to run safe functions on values within obj } }; function toSafeDate(value) { if (value === undefined || value === null || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) !== _typeof('') && !(value instanceof Date)) { return null; } else { return String(new Date(value).toISOString()); } }; function toSafeData(value) { if (value === undefined || value === null || !(value instanceof Buffer)) { return null; } else { return value; } }; function hasRequiredValues(requiredValues, obj, parentName) { var allPresent = true; for (var i = 0, iLength = requiredValues.length; i < iLength; i += 1) { if (obj[requiredValues[i]] === undefined || obj[requiredValues[i]] === null) { allPresent = false; throw new Error('Missing ' + requiredValues[i] + ' from ' + parentName); } } return allPresent; };