All files / modules/10-common/utils StringUtils.ts

97.3% Statements 36/37
60% Branches 6/10
85.71% Functions 6/7
97.22% Lines 35/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111              694x     694x 134x           694x                                                 694x 694x 694x       694x     694x   694x   694x   694x   694x     694x   694x   694x   694x   694x   694x   694x 694x 19x       694x     49x     694x 52x           694x 982x         694x   694x   694x 4x 1x   3x 3x    
/*
 * Copyright 2022 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import isUndefined from 'lodash/isUndefined'
import type { GitSyncEntityDTO } from 'services/cd-ng'
 
export function getIdentifierFromName(str: string): string {
  return str
    .trim()
    .replace(/[^0-9a-zA-Z_$ ]/g, '') // remove special chars except _ and $
    .replace(/ +/g, '_') // replace spaces with _
}
 
export const illegalIdentifiers = [
  'or',
  'and',
  'eq',
  'ne',
  'lt',
  'gt',
  'le',
  'ge',
  'div',
  'mod',
  'not',
  'null',
  'true',
  'false',
  'new',
  'var',
  'return',
  'step',
  'parallel',
  'stepGroup',
  'org',
  'account'
]
 
export const DEFAULT_DATE_FORMAT = 'MM/DD/YYYY hh:mm a'
export const DATE_WITHOUT_TIME_FORMAT = 'MMMM Do YYYY'
export function pluralize(number: number): string {
  return number > 1 || number === 0 ? 's' : ''
}
 
export const regexEmail =
  /^(([^<>()\\[\]\\.,;:\s@"]+(\.[^<>()\\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
 
export const regexName = /^[A-Za-z0-9_-][A-Za-z0-9 _-]*$/
 
export const regexPositiveNumbers = /^[1-9]+[0-9]*$/
 
export const regexIdentifier = /^[a-zA-Z_.][0-9a-zA-Z_$]*$/
 
export const keyRegexIdentifier = /^([a-zA-Z_])([0-9a-zA-Z_$]*)+((\.[0-9a-zA-Z_$]+)*)$/
 
export const k8sLabelRegex = /[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/
 
// https://kubernetes.io/docs/concepts/overview/working-with-objects/annotations/#syntax-and-character-set
export const k8sAnnotationRegex = /^[a-z0-9A-Z]([a-z0-9A-Z-_./])*[a-z0-9A-Z]$/
 
export const regexVersionLabel = /^[0-9a-zA-Z][\S]*$/
 
export const HarnessFolderName = /^[A-Za-z0-9_\-/][A-Za-z0-9 _\-/]*$/
 
export const GitSuffixRegex = /.git(\/)*$/g
 
export const folderPathName = /^[A-Za-z0-9_-][A-Za-z0-9 _-]*$/g
 
export const yamlFileExtension = '.yaml'
 
export const UNIQUE_ID_MAX_LENGTH = 64
export function toVariableStr(str: string): string {
  return `<+${str}>`
}
 
// adopted from https://github.com/sindresorhus/escape-string-regexp v5.0.0
export function escapeStringRegexp(str: string): string {
  // Escape characters with special meaning either inside or outside character sets.
  // Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
  return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d')
}
 
export function sanitizeHTML(str: string): string {
  return str
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/\u00a0/g, ' ')
}
 
export function getEntityNameFromType(entity: GitSyncEntityDTO['entityType']): string {
  return !isUndefined(entity)
    ? (entity.endsWith('s') ? entity.substring(0, entity.length - 1) : entity).toLowerCase()
    : ''
}
 
export const serviceDependencyIdRegex = /^[a-zA-Z][a-zA-Z0-9_]*$/
 
export const portNumberRegex = /^[0-9]*$/
 
export const joinAsASentence = (items: string[]): string => {
  if (!items.length) {
    return ''
  }
  const last = items.pop()
  return items.join(', ') + ' and ' + last
}