Advertisement
Guest User

.clone() dummy implementation

a guest
Apr 25th, 2024
60
0
15 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 2.61 KB | Source Code | 0 0
  1. export const globals = {};
  2.  
  3. declare global {
  4.   interface Array<T> {
  5.     clone<T>(this: T[], options?: CloneOptions<T>, ...args: any): T[];
  6.   }
  7. }
  8.  
  9. interface TransformerParams<T> {
  10.   item: T;
  11.   index: number;
  12.   array: T[];
  13.   original: T[];
  14.   args: any[];
  15. }
  16.  
  17. interface CloneOptions<T> {
  18.   transformer: (params: TransformerParams<T>) => boolean;
  19. }
  20.  
  21. Array.prototype.clone = function <T>(
  22.   this: T[],
  23.   options?: CloneOptions<T>,
  24.   ...args: unknown[]
  25. ) {
  26.   if (!options) {
  27.     return this.slice();
  28.   }
  29.  
  30.   const result: T[] = [];
  31.   const { transformer }: CloneOptions<T> = options;
  32.   let continueLoop = false;
  33.   let i = 0;
  34.  
  35.   do {
  36.     if (i >= this.length) {
  37.       break;
  38.     }
  39.  
  40.     continueLoop = transformer({
  41.       array: result,
  42.       index: i,
  43.       item: this[i],
  44.       original: this,
  45.       args,
  46.     });
  47.  
  48.     i++;
  49.   } while (continueLoop);
  50.  
  51.   return result;
  52. };
  53.  
  54. /**
  55.  * Dummy reverse implementation
  56.  */
  57. function reverse<T>({ index, original, array }: TransformerParams<T>) {
  58.   array.push(original[original.length - 1 - index]);
  59.   return true;
  60. }
  61.  
  62. /**
  63.  * Dummy splice implementation
  64.  */
  65. function splice<T>({
  66.   index,
  67.   original,
  68.   array,
  69.   args: [start, deleteCount = Number.MAX_SAFE_INTEGER, ...items],
  70. }: TransformerParams<T>): boolean {
  71.   if (start < 0) {
  72.     while (start < 0) {
  73.       start += original.length;
  74.     }
  75.   } else if (start >= original.length) {
  76.     return !array.push(...original, ...items);
  77.   }
  78.  
  79.   if (index < start) {
  80.     // Always return `true`
  81.     return !!array.push(original[index]);
  82.   }
  83.  
  84.   array.push(...items);
  85.   index += deleteCount;
  86.  
  87.   for (; index < original.length; index++) {
  88.     array.push(original[index]);
  89.   }
  90.  
  91.   return false;
  92. }
  93.  
  94. const original = ["Jan", "March", "April", "June"];
  95. const clone1 = original.clone();
  96. const multipliedBy2clone = original.clone({
  97.   transformer: ({ item, array }) => !!array.push(item + "_"),
  98. });
  99. const reversedClone = original.clone({ transformer: reverse });
  100. const splicedClone = original.clone({ transformer: splice }, 3, 0, "Feb");
  101. const splicedClone1 = original.clone({ transformer: splice }, 1, 2, "hola");
  102.  
  103. original[1] = "99";
  104. clone1[0] = "333";
  105.  
  106. console.log({
  107.   original,
  108.   clone1,
  109.   multipliedBy2clone,
  110.   reversedClone,
  111.   splicedClone,
  112.   splicedClone1,
  113. });
  114.  
  115. /**
  116. {
  117.   original: [ "Jan", "99", "April", "June" ],
  118.   clone1: [ "333", "March", "April", "June" ],
  119.   multipliedBy2clone: [ "Jan_", "March_", "April_", "June_" ],
  120.   reversedClone: [ "June", "April", "March", "Jan" ],
  121.   splicedClone: [ "Jan", "March", "April", "Feb", "June" ],
  122.   splicedClone1: [ "Jan", "hola", "June" ]
  123. }
  124.  */
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement