All files / modules/75-cf/utils instructions.ts

97.75% Statements 87/89
72.73% Branches 8/11
96.43% Functions 27/28
97.5% Lines 78/80

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501              14x                                                                                                                                                                                                                                                                                                                           14x 14x 410x 28x 14x 14x 14x       337x       14x 14x       141x       14x 14x       15x         14x       14x       14x       14x       14x       14x 14x       14x       14x       14x       14x       14x       14x       14x         14x       14x             14x             14x       14x       14x           5x           14x       14x       14x         14x 14x         14x       14x       14x         14x       14x       14x       14x       14x         14x 14x         14x                   1x                 1x                     28x       22x               23x           9x       21x 18x   21x       8x 2x   8x       27x               14x       14x 14x     14x               14x       14x 14x     14x       14x 46x 46x 8x     14x                                                                                      
/*
 * Copyright 2021 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 { zipObject, isEqual } from 'lodash-es'
import type { Distribution, PatchOperation, Prerequisite, Serve, Variation, WeightedVariation } from 'services/cf'
 
type PatchKind =
  | 'updateName'
  | 'updateDescription'
  | 'addPrerequisite'
  | 'updatePrerequisite'
  | 'removePrerequisite'
  | 'addVariation'
  | 'updateVariation'
  | 'deleteVariation'
  | 'setDefaultOnVariation'
  | 'setDefaultOffVariation'
  | 'addTag'
  | 'updateTag'
  | 'removeTag'
  | 'setFeatureFlagState'
  | 'addTargetsToVariationTargetMap'
  | 'removeTargetsToVariationTargetMap'
  | 'addSegmentToVariationTargetMap'
  | 'removeSegmentToVariationTargetMap'
  | 'clearVariationTargetMapping'
  | 'addRule'
  | 'removeRule'
  | 'addClause'
  | 'removeClause'
  | 'updateClause'
  | 'reorderRules'
  | 'updateDefaultServe'
  | 'updateOffVariation'
  | 'addToIncludeList'
  | 'removeFromIncludeList'
  | 'addToExcludeList'
  | 'removeFromExcludeList'
  | 'updateRule'
  | 'updatePermanent'
 
export type PrerequisiteFeature = Pick<Prerequisite, 'feature'>
export type VariationIdentifier = Pick<Variation, 'identifier'>[]
export interface TagParams {
  name: string
  value: any
}
export interface VariationParam {
  variation: string
}
export interface UpdateNameParams {
  name: string
}
export interface UpdateDescriptionParams {
  description: string
}
export interface SetStateParams {
  state: 'on' | 'off'
}
export interface UpdatePermanentParams {
  state: boolean
}
export interface TargetToVariationParams {
  variation: string
  targets: string[]
}
export interface SegmentToVariationParams {
  variation: string
  targetSegments: string[]
}
export interface ClauseData {
  attribute?: string
  op: string
  values: string[]
  negate?: boolean
}
export interface AddRuleParams {
  priority: number
  serve: Serve
  clauses: ClauseData[]
  uuid?: string
}
export interface RemoveRuleParams {
  ruleID: string
}
export interface AddClauseParams {
  ruleID: string
  clause: ClauseData
}
export interface RemoveClauseParams {
  ruleID: string
  clauseID: string
}
export interface UpdateClauseParams {
  ruleID: string
  clauseID: string
  clause: ClauseData[]
}
export interface ReorderRulesParams {
  rules: string[]
}
type UpdateDefaultServeParams = VariationParam | { bucketID: string; variations: WeightedVariation[] }
type TargetList = { targets: string[] }
 
export type UpdateRuleVariation =
  | {
      ruleID: string
      variation: string
    }
  | {
      ruleID: string
      bucketBy: string
      variations: WeightedVariation[]
    }
 
export type AddClauseToSegmentParams = ClauseData
export type UpdateClauseOnSegmentParams = ClauseData & {
  clauseID: string
}
export type RemoveClauseOnSegmentParams = { clauseID: string }
 
type ParameterType =
  | VariationParam
  | UpdateNameParams
  | UpdateDescriptionParams
  | SetStateParams
  | Distribution
  | Prerequisite
  | PrerequisiteFeature
  | Variation
  | VariationIdentifier
  | TagParams
  | TargetToVariationParams
  | SegmentToVariationParams
  | AddRuleParams
  | RemoveRuleParams
  | AddClauseParams
  | RemoveClauseParams
  | UpdateClauseParams
  | ReorderRulesParams
  | UpdateDefaultServeParams
  | TargetList
  | AddClauseToSegmentParams
  | UpdateClauseOnSegmentParams
  | RemoveClauseOnSegmentParams
  | UpdateRuleVariation
  | UpdatePermanentParams
 
export interface Instruction<Params extends ParameterType = ParameterType> {
  kind: PatchKind
  parameters: Params
}
 
type UnaryParameterBuilder<A, B extends ParameterType> = (a: A) => B
type BinaryParameterBuilder<A, B, C extends ParameterType> = (a: A, b: B) => C
type TernaryParameterBuilder<A, B, C, D extends ParameterType> = (a: A, b: B, c: C) => D
 
type UnaryInstructionCreator<A, B extends ParameterType> = (a: A) => Instruction<B>
type BinaryInstructionCreator<A, B, C extends ParameterType> = (a: A, b: B) => Instruction<C>
type TernaryInstructionCreator<A, B, C, D extends ParameterType> = (a: A, b: B, c: C) => Instruction<D>
 
export const shape =
  <A = any>(...keys: string[]) =>
  (...values: any[]) =>
    zipObject(keys, values) as A
const identity = <T>(a: T) => a
export const unaryInstructionCreator =
  <A, B extends ParameterType>(
    kind: PatchKind,
    parameterBuilder: UnaryParameterBuilder<A, B>
  ): UnaryInstructionCreator<A, B> =>
  (arg: A) => ({
    kind,
    parameters: parameterBuilder(arg)
  })
export const binaryInstructionCreator =
  <A, B, C extends ParameterType>(
    kind: PatchKind,
    parameterBuilder: BinaryParameterBuilder<A, B, C>
  ): BinaryInstructionCreator<A, B, C> =>
  (a: A, b: B) => ({
    kind,
    parameters: parameterBuilder(a, b)
  })
export const ternaryInstructionCreator =
  <A, B, C, D extends ParameterType>(
    kind: PatchKind,
    parameterBuilder: TernaryParameterBuilder<A, B, C, D>
  ): TernaryInstructionCreator<A, B, C, D> =>
  (a: A, b: B, c: C) => ({
    kind,
    parameters: parameterBuilder(a, b, c)
  })
 
const updateName: (name: string) => Instruction<UpdateNameParams> = unaryInstructionCreator(
  'updateName',
  shape<UpdateNameParams>('name')
)
const updateDescription: (description: string) => Instruction<UpdateDescriptionParams> = unaryInstructionCreator(
  'updateDescription',
  shape<UpdateDescriptionParams>('description')
)
const addPrerequisite: (prerequisite: Prerequisite) => Instruction<Prerequisite> = unaryInstructionCreator(
  'addPrerequisite',
  identity
)
const updatePrequisite: (prerequisites: Prerequisite) => Instruction<Prerequisite> = unaryInstructionCreator(
  'updatePrerequisite',
  identity
)
const removePrerequisite: (prerequisiteFeature: string) => Instruction<PrerequisiteFeature> = unaryInstructionCreator(
  'removePrerequisite',
  shape('feature')
)
const addVariation: (variation: Variation) => Instruction<Variation> = unaryInstructionCreator('addVariation', identity)
const updateVariation: (variation: Variation) => Instruction<Variation> = unaryInstructionCreator(
  'updateVariation',
  identity
)
const deleteVariant: (identifier: string | undefined) => Instruction<VariationIdentifier> = unaryInstructionCreator(
  'deleteVariation',
  shape<VariationIdentifier>('identifier')
)
const setDefaultOnVariation: (identifier: string) => Instruction<VariationIdentifier> = unaryInstructionCreator(
  'setDefaultOnVariation',
  shape<VariationIdentifier>('identifier')
)
const setDefaultOffVariation: (identifier: string) => Instruction<VariationIdentifier> = unaryInstructionCreator(
  'setDefaultOffVariation',
  shape<VariationIdentifier>('identifier')
)
const addTag: (name: string, value: any) => Instruction<TagParams> = binaryInstructionCreator(
  'addTag',
  shape<TagParams>('name', 'value')
)
const updateTag: (name: string, value: any) => Instruction<TagParams> = binaryInstructionCreator(
  'updateTag',
  shape<TagParams>('name', 'value')
)
const removeTag: (name: string, value: any) => Instruction<TagParams> = binaryInstructionCreator(
  'removeTag',
  shape<TagParams>('name', 'value')
)
const setFeatureFlagState: (state: 'on' | 'off') => Instruction<SetStateParams> = unaryInstructionCreator(
  'setFeatureFlagState',
  shape<SetStateParams>('state')
)
const addTargetsToVariationTargetMap: (variation: string, targets: string[]) => Instruction<TargetToVariationParams> =
  binaryInstructionCreator('addTargetsToVariationTargetMap', shape<TargetToVariationParams>('variation', 'targets'))
const removeTargetsToVariationTargetMap: (
  variation: string,
  targets: string[]
) => Instruction<TargetToVariationParams> = binaryInstructionCreator(
  'removeTargetsToVariationTargetMap',
  shape<TargetToVariationParams>('variation', 'targets')
)
const addSegmentToVariationTargetMap: (
  variation: string,
  targetSegments: string[]
) => Instruction<TargetToVariationParams> = binaryInstructionCreator(
  'addSegmentToVariationTargetMap',
  shape<TargetToVariationParams>('variation', 'targetSegments')
)
const removeSegmentToVariationTargetMap: (
  variation: string,
  targetSegments: string[]
) => Instruction<SegmentToVariationParams> = binaryInstructionCreator(
  'removeSegmentToVariationTargetMap',
  shape<SegmentToVariationParams>('variation', 'targetSegments')
)
const clearVariationTargetMapping: (variation: string) => Instruction<VariationParam> = unaryInstructionCreator(
  'clearVariationTargetMapping',
  shape<VariationParam>('variation')
)
const addRule = (addRuleParamData: {
  uuid?: string
  priority: number
  serve: Serve
  clauses: ClauseData[]
}): Instruction<AddRuleParams> => {
  return {
    kind: 'addRule',
    parameters: addRuleParamData
  }
}
 
const removeRule: (ruleID: string) => Instruction<RemoveRuleParams> = unaryInstructionCreator(
  'removeRule',
  shape('ruleID')
)
const addClause: (ruleID: string, clause: ClauseData) => Instruction<AddClauseParams> = binaryInstructionCreator(
  'addClause',
  shape('ruleID', 'clause')
)
const removeClause: (ruleID: string, clauseID: string) => Instruction<RemoveClauseParams> = binaryInstructionCreator(
  'removeClause',
  shape('ruleID', 'clauseID')
)
const updateClause: (ruleID: string, clauseID: string, clause: ClauseData) => Instruction<UpdateClauseParams> =
  ternaryInstructionCreator('updateClause', shape('ruleID', 'clauseID', 'clause'))
const reorderRules: (rules: string[]) => Instruction<ReorderRulesParams> = unaryInstructionCreator(
  'reorderRules',
  shape<ReorderRulesParams>('rules')
)
const updateDefaultServeByVariation: (variation: string) => Instruction<UpdateDefaultServeParams> =
  unaryInstructionCreator('updateDefaultServe', shape<UpdateDefaultServeParams>('variation'))
const updateDefaultServeByBucket: (
  bucketBy: string,
  variations: WeightedVariation[]
) => Instruction<UpdateDefaultServeParams> = binaryInstructionCreator(
  'updateDefaultServe',
  shape<UpdateDefaultServeParams>('bucketBy', 'variations')
)
const updateOffVariation: (variation: string) => Instruction<VariationParam> = unaryInstructionCreator(
  'updateOffVariation',
  shape('variation')
)
 
const addToIncludeList: (targets: string[]) => Instruction<TargetList> = unaryInstructionCreator(
  'addToIncludeList',
  shape<TargetList>('targets')
)
const removeFromIncludeList: (targets: string[]) => Instruction<TargetList> = unaryInstructionCreator(
  'removeFromIncludeList',
  shape<TargetList>('targets')
)
const addToExcludeList: (targets: string[]) => Instruction<TargetList> = unaryInstructionCreator(
  'addToExcludeList',
  shape<TargetList>('targets')
)
const removeFromExcludeList: (targets: string[]) => Instruction<TargetList> = unaryInstructionCreator(
  'removeFromExcludeList',
  shape<TargetList>('targets')
)
const addClauseToSegment: (clause: ClauseData) => Instruction<AddClauseToSegmentParams> = unaryInstructionCreator(
  'addClause',
  identity
)
const updateClauseOnSegment: (clause: UpdateClauseOnSegmentParams) => Instruction<UpdateClauseOnSegmentParams> =
  unaryInstructionCreator('updateClause', identity)
const removeClauseOnSegment: (clauseID: string) => Instruction<RemoveClauseOnSegmentParams> = unaryInstructionCreator(
  'removeClause',
  shape<RemoveClauseOnSegmentParams>('clauseID')
)
 
const updatePermanent: (state: boolean) => Instruction<UpdatePermanentParams> = unaryInstructionCreator(
  'updatePermanent',
  shape<UpdatePermanentParams>('permanent')
)
function updateRuleVariation(ruleID: string, variation: string): Instruction<UpdateRuleVariation>
function updateRuleVariation(
  ruleID: string,
  variation: { bucketBy: string; variations: WeightedVariation[] }
): Instruction<UpdateRuleVariation>
function updateRuleVariation(ruleID: string, variation: any): Instruction<UpdateRuleVariation> {
  Iif (typeof variation === 'string') {
    return {
      kind: 'updateRule',
      parameters: {
        ruleID,
        variation
      }
    }
  } else {
    return {
      kind: 'updateRule',
      parameters: {
        ruleID,
        ...variation
      }
    }
  }
}
 
class SemanticPatch {
  instructions: any[] = []
 
  addInstruction(instruction: Instruction): void {
    // logic
    this.instructions.push(instruction)
  }
 
  toJSON(): string {
    return JSON.stringify(this.instructions)
  }
 
  toPatchOperation(): PatchOperation {
    return {
      instructions: this.instructions
    }
  }
 
  addAllInstructions(instructions: Instruction[]) {
    this.instructions.push(...instructions)
  }
 
  onPatchAvailable(fn: (p: PatchOperation) => void): SemanticPatch {
    if (this.instructions.length !== 0) {
      fn(this.toPatchOperation())
    }
    return this
  }
 
  onEmptyPatch(fn: () => void): SemanticPatch {
    if (this.instructions.length === 0) {
      fn()
    }
    return this
  }
 
  reset(): void {
    this.instructions = []
  }
}
 
class FeatureSemanticPatch extends SemanticPatch {
  private static instance: FeatureSemanticPatch
 
  private constructor() {
    super()
  }
 
  public static getInstance(): FeatureSemanticPatch {
    Eif (!FeatureSemanticPatch.instance) {
      FeatureSemanticPatch.instance = new FeatureSemanticPatch()
    }
 
    return FeatureSemanticPatch.instance
  }
}
 
class SegmentSemanticPatch extends SemanticPatch {
  private static instance: SegmentSemanticPatch
 
  private constructor() {
    super()
  }
 
  public static getInstance(): SegmentSemanticPatch {
    Eif (!SegmentSemanticPatch.instance) {
      SegmentSemanticPatch.instance = new SegmentSemanticPatch()
    }
 
    return SegmentSemanticPatch.instance
  }
}
 
export const getDiff = <A, B>(initial: A[], updated: B[], eqFn: (a: A, b: B) => boolean = isEqual) => {
  const newData = updated.filter(b => !initial.find(a => eqFn(a, b)))
  const remData = initial.filter(a => !updated.find(b => eqFn(a, b)))
  return [newData, remData]
}
 
export default {
  feature: FeatureSemanticPatch.getInstance(),
  segment: SegmentSemanticPatch.getInstance(),
  creators: {
    updateName,
    updateDescription,
    addPrerequisite,
    updatePrequisite,
    updatePermanent,
    removePrerequisite,
    addVariation,
    updateVariation,
    deleteVariant,
    setDefaultOnVariation,
    setDefaultOffVariation,
    addTag,
    updateTag,
    removeTag,
    setFeatureFlagState,
    addTargetsToVariationTargetMap,
    removeTargetsToVariationTargetMap,
    addSegmentToVariationTargetMap,
    removeSegmentToVariationTargetMap,
    clearVariationTargetMapping,
    addRule,
    removeRule,
    addClause,
    removeClause,
    updateClause,
    reorderRules,
    updateDefaultServeByVariation,
    updateDefaultServeByBucket,
    updateOffVariation,
    addToIncludeList,
    removeFromIncludeList,
    addToExcludeList,
    removeFromExcludeList,
    addClauseToSegment,
    updateClauseOnSegment,
    removeClauseOnSegment,
    updateRuleVariation
  }
}