@rh-support/utils
Advanced tools
Comparing version 0.0.52 to 0.0.53
@@ -197,2 +197,93 @@ define(['exports', 'lodash/isEmpty', 'lodash/includes', 'lodash/filter', 'lodash/forEach', 'lodash/map', 'lodash/isFunction', 'js-markdown-extra', 'lodash/assign', 'lodash/get', 'lodash/isString', 'marked', 'lodash/orderBy', 'lodash/uniq', 'lodash/zipObject', 'solr-query-builder', 'qs'], function (exports, isEmpty, includes, filter, forEach, map, isFunction, jsMarkdownExtra, assign, get, isString, marked, orderBy, uniq, zipObject, SolrQueryBuilder, qs) { 'use strict'; | ||
// Most of it is copied from https://github.com/react-csv/react-csv/blob/master/src/core.js | ||
// except we add the transform a column functionality and also we escape the double quotes | ||
// for every value so that we dont break the csv | ||
/** | ||
* Simple safari detection based on user agent test | ||
*/ | ||
const isSafari = () => /^((?!chrome|android).)*safari/i.test(navigator.userAgent); | ||
const escapeDoubleQuotes = (data) => data && typeof data === 'string' ? data.replace(/"/g, '""') : data; | ||
const defaultTransformFn = (data) => data; | ||
const isJsons = ((array) => Array.isArray(array) && array.every(row => (typeof row === 'object' && !(row instanceof Array)))); | ||
const isArrays = ((array) => Array.isArray(array) && array.every(row => Array.isArray(row))); | ||
const jsonsHeaders = ((array) => Array.from(array.map(json => Object.keys(json)) | ||
.reduce((a, b) => new Set([...a, ...b]), []))); | ||
const jsons2arrays = (jsons, headers) => { | ||
headers = headers || jsonsHeaders(jsons); | ||
// allow headers to have custom labels, defaulting to having the header data key be the label | ||
let headerLabels = headers; | ||
let headerKeys = headers; | ||
if (isJsons(headers)) { | ||
headerLabels = headers.map((header) => header.label); | ||
headerKeys = headers.map((header) => ({ key: header['key'], transformFn: header['transform'] })); | ||
} | ||
const data = jsons.map((object) => headerKeys.map((header) => getHeaderValue(header.key, object, header.transformFn))); | ||
return [headerLabels, ...data]; | ||
}; | ||
const getHeaderValue = (property, obj, transformFn = defaultTransformFn) => { | ||
const foundValue = property | ||
.replace(/\[([^\]]+)]/g, '.$1') | ||
.split('.') | ||
.reduce(function (o, p, i, arr) { | ||
// if at any point the nested keys passed do not exist, splice the array so it doesnt keep reducing | ||
if (o[p] === undefined) { | ||
arr.splice(1); | ||
} | ||
else { | ||
return escapeDoubleQuotes(o[p]); | ||
} | ||
}, obj); | ||
// if at any point the nested keys passed do not exist then looks for key `property` in object obj | ||
return (foundValue === undefined) ? ((property in obj) ? escapeDoubleQuotes(obj[property]) : '') : transformFn(foundValue); | ||
}; | ||
const elementOrEmpty = (element) => element || element === 0 ? element : ''; | ||
const joiner = ((data, separator = ',', enclosingCharacter = '"') => { | ||
return data | ||
.filter(e => e) | ||
.map(row => row | ||
.map((element) => elementOrEmpty(element)) | ||
.map(column => `${enclosingCharacter}${column}${enclosingCharacter}`) | ||
.join(separator)) | ||
.join(`\n`); | ||
}); | ||
const arrays2csv = ((data, headers, separator, enclosingCharacter) => joiner(headers ? [headers, ...data] : data, separator, enclosingCharacter)); | ||
const jsons2csv = ((data, headers, separator, enclosingCharacter) => joiner(jsons2arrays(data, headers), separator, enclosingCharacter)); | ||
const string2csv = ((data, headers, separator = ',', enclosingCharacter = '"') => (headers) ? `${headers.join(separator)}\n${data}` : data); | ||
const toCSV = (data, headers, separator = ',', enclosingCharacter = '"') => { | ||
if (isJsons(data)) | ||
return jsons2csv(data, headers, separator, enclosingCharacter); | ||
if (isArrays(data)) | ||
return arrays2csv(data, headers, separator, enclosingCharacter); | ||
if (typeof data === 'string') | ||
return string2csv(data, headers, separator); | ||
throw new TypeError(`Data should be a "String", "Array of arrays" OR "Array of objects" `); | ||
}; | ||
const buildURI = ((data, headers, uFEFF = true, separator = ',', enclosingCharacter = '"') => { | ||
const csv = toCSV(data, headers, separator, enclosingCharacter); | ||
const type = isSafari() ? 'application/csv' : 'text/csv'; | ||
const blob = new Blob([uFEFF ? '\uFEFF' : '', csv], { type }); | ||
const dataURI = `data:${type};charset=utf-8,${uFEFF ? '\uFEFF' : ''}${csv}`; | ||
const URL = window.URL || window.webkitURL; | ||
return (typeof URL.createObjectURL === 'undefined') | ||
? dataURI | ||
: URL.createObjectURL(blob); | ||
}); | ||
const downloadCSV = (data, headers, fileName = 'csvGenerator', uFEFF = true, separator = ',', enclosingCharacter = '"') => { | ||
// If this browser is IE 11, it does not support the `download` attribute | ||
if (window.navigator.msSaveOrOpenBlob) { | ||
let blob = new Blob([uFEFF ? '\uFEFF' : '', toCSV(data, headers, separator, enclosingCharacter)]); | ||
window.navigator.msSaveBlob(blob, fileName); | ||
} | ||
else { | ||
const uri = buildURI(data, headers); | ||
const element = document.createElement('a'); | ||
element.setAttribute('href', uri); | ||
element.setAttribute('target', '_self'); | ||
element.setAttribute('download', fileName); | ||
document.body.appendChild(element); | ||
element.click(); | ||
document.body.removeChild(element); | ||
} | ||
}; | ||
const formatDate = (date, locale = 'en-us', format = { month: 'short', day: 'numeric', year: 'numeric' }) => { | ||
@@ -551,3 +642,5 @@ if (!(date instanceof Date)) { | ||
exports.CacheUtilsService = CacheUtilsService; | ||
exports.arrays2csv = arrays2csv; | ||
exports.buildSolrQuery = buildSolrQuery; | ||
exports.buildURI = buildURI; | ||
exports.canManageCase = canManageCase; | ||
@@ -558,5 +651,9 @@ exports.cleanupMarkDown = cleanupMarkDown; | ||
exports.defaultMarkdownOptions = defaultMarkdownOptions; | ||
exports.downloadCSV = downloadCSV; | ||
exports.elementOrEmpty = elementOrEmpty; | ||
exports.escapeDoubleQuotes = escapeDoubleQuotes; | ||
exports.formatDate = formatDate; | ||
exports.getApiResourceObject = getApiResourceObject; | ||
exports.getDropdownBtnPlaceholder = getDropdownBtnPlaceholder; | ||
exports.getHeaderValue = getHeaderValue; | ||
exports.getRecommendationAbstract = getRecommendationAbstract; | ||
@@ -569,5 +666,12 @@ exports.getRecommendationTitle = getRecommendationTitle; | ||
exports.humanizeSize = humanizeSize; | ||
exports.isArrays = isArrays; | ||
exports.isFirefox = isFirefox; | ||
exports.isJsons = isJsons; | ||
exports.isPremiumEntitlement = isPremiumEntitlement; | ||
exports.isSafari = isSafari; | ||
exports.isSev1 = isSev1; | ||
exports.joiner = joiner; | ||
exports.jsons2arrays = jsons2arrays; | ||
exports.jsons2csv = jsons2csv; | ||
exports.jsonsHeaders = jsonsHeaders; | ||
exports.markdownToHTML = markdownToHTML; | ||
@@ -580,2 +684,4 @@ exports.replaceHighlightingData = replaceHighlightingData; | ||
exports.solrResponseToPivotFields = solrResponseToPivotFields; | ||
exports.string2csv = string2csv; | ||
exports.toCSV = toCSV; | ||
exports.toOption = toOption; | ||
@@ -582,0 +688,0 @@ exports.toOptions = toOptions; |
export * from './apiUtils'; | ||
export * from './caseUtils'; | ||
export * from './cacheUtils'; | ||
export * from './csvUtils'; | ||
export * from './dateUtils'; | ||
@@ -5,0 +6,0 @@ export * from './dropdownUtils'; |
export * from './apiUtils'; | ||
export * from './caseUtils'; | ||
export * from './cacheUtils'; | ||
export * from './csvUtils'; | ||
export * from './dateUtils'; | ||
@@ -5,0 +6,0 @@ export * from './dropdownUtils'; |
{ | ||
"name": "@rh-support/utils", | ||
"version": "0.0.52", | ||
"version": "0.0.53", | ||
"description": "> TODO: description", | ||
@@ -56,4 +56,4 @@ "author": "Vikas Rathee <vrathee@redhat.com>", | ||
"dependencies": { | ||
"@rh-support/api": "^0.0.52", | ||
"@rh-support/types": "^0.0.52", | ||
"@rh-support/api": "^0.0.53", | ||
"@rh-support/types": "^0.0.53", | ||
"js-markdown-extra": "^1.2.4", | ||
@@ -72,3 +72,3 @@ "localforage": "^1.7.3", | ||
}, | ||
"gitHead": "faf05fce6088743ce4fd85e479ccb2f45d15ff43" | ||
"gitHead": "0bb826b4998339ed5de4951c6ec91e18197cfdf7" | ||
} |
68753
29
1456
+ Added@rh-support/api@0.0.53(transitive)
+ Added@rh-support/types@0.0.53(transitive)
- Removed@rh-support/api@0.0.52(transitive)
- Removed@rh-support/types@0.0.52(transitive)
Updated@rh-support/api@^0.0.53
Updated@rh-support/types@^0.0.53