<commit_before>import { format } from 'util';
import { Type, reflector, Provider } from '@ts-stack/di';

import { isModuleWithOptions, isModule, isRootModule, isProvider } from './utils/type-guards';
import { ModuleWithOptions, ModuleDecorator, Module } from './decorators/module';
import { mergeArrays } from './utils/merge-arrays-options';
import { RootModule } from './decorators/root-module';
import { deepFreeze } from './utils/deep-freeze';
import { normalizeProviders } from './utils/ng-utils';

export abstract class Factory {
  protected throwProvidersCollisionError(moduleName: string, duplicates: any[]) {
    const names = duplicates.map((p) => p.name || p).join(', ');
    const provider = duplicates.length > 1 ? 'these providers' : 'this provider';
    throw new Error(
      `Exporting providers in ${moduleName} was failed: Collision was found for: ${names}. You should manually add ${provider} to ${moduleName}.`
    );
  }

  protected getModule(mod: Type<any> | ModuleWithOptions<any>) {
    return isModuleWithOptions(mod) ? mod.module : mod;
  }

  protected getModuleName(modOrObject: Type<any> | ModuleWithOptions<any>) {
    return isModuleWithOptions(modOrObject) ? modOrObject.module.name : modOrObject.name;
  }

  protected checkModuleMetadata(modMetadata: ModuleDecorator, modName: string, isRoot?: boolean) {
    const decoratorName = isRoot ? '@RootModule' : '@Module';

    if (!modMetadata) {
      throw new Error(`Module build failed: module "${modName}" does not have the "${decoratorName}()" decorator`);
    }
  }

  protected getRawModuleMetadata<T extends ModuleDecorator>(
    modOrObject: Type<any> | ModuleWithOptions<any>,
    isRoot?: boolean
  ) {
    let modMetadata: T;
    const typeGuard = isRoot ? isRootModule : (m: ModuleDecorator) => isModule(m) || isRootModule(m);
    const Metadata = isRoot ? RootModule : Module;

    if (isModuleWithOptions(modOrObject)) {
      const modWitOptions = modOrObject;
      modMetadata = reflector.annotations(modWitOptions.module).find(typeGuard) as T;
      const modName = this.getModuleName(modWitOptions.module);
      this.checkModuleMetadata(modMetadata, modName, isRoot);

      const metadata = new Metadata(modMetadata);
      metadata.providersPerApp = mergeArrays(modMetadata.providersPerApp, modWitOptions.providersPerApp);
      metadata.providersPerMod = mergeArrays(modMetadata.providersPerMod, modWitOptions.providersPerMod);
      metadata.providersPerReq = mergeArrays(modMetadata.providersPerReq, modWitOptions.providersPerReq);
      return deepFreeze(metadata);
    } else {
      return deepFreeze<T>(reflector.annotations(modOrObject).find(typeGuard));
    }
  }

  /**
   * Returns last provider if the provider has the duplicate.
   */
  protected getUniqProviders(providers: Provider[]) {
    const tokens = normalizeProviders(providers).map((np) => np.provide);
    const uniqProviders: Provider[] = [];

    tokens.forEach((currToken, currIndex) => {
      if (tokens.lastIndexOf(currToken) == currIndex) {
        uniqProviders.push(providers[currIndex]);
      }
    });

    return uniqProviders;
  }

  /**
   * Returns array of uniq tokens.
   */
  protected getTokensCollisions(uniqDuplTokens: any[], providers: Provider[]) {
    uniqDuplTokens = uniqDuplTokens || [];
    providers = providers || [];
    const duplProviders: Provider[] = [];

    normalizeProviders(providers)
      .map((np) => np.provide)
      .forEach((currToken, currIndex) => {
        if (uniqDuplTokens.includes(currToken)) {
          duplProviders.push(providers[currIndex]);
        }
      });

    const normDuplProviders = normalizeProviders(duplProviders);

    return uniqDuplTokens.filter((dulpToken) => {
      let prevProvider: Provider;

      for (let i = 0; i < normDuplProviders.length; i++) {
        if (normDuplProviders[i].provide !== dulpToken) {
          continue;
        }

        const currProvider = duplProviders[i];
        if (!prevProvider) {
          prevProvider = currProvider;
        }

        // If we have a replacement for some provider - this is a collision.

        if (isProvider(prevProvider) && isProvider(currProvider)) {
          if (prevProvider.provide !== currProvider.provide || format(prevProvider) != format(currProvider)) {
            return true;
          }
          continue;
        }
        // TODO Remove it after a while.
        // else if (prevProvider !== currProvider) {
        //   return true;
        // }
        throw new TypeError(`Unrecognized providers found: ${prevProvider} and ${currProvider}`);
      }
    });
  }
}
<commit_msg>chore(core): fixed description for Facroty's getTokensCollisions()<commit_after>import { format } from 'util';
import { Type, reflector, Provider } from '@ts-stack/di';

import { isModuleWithOptions, isModule, isRootModule, isProvider } from './utils/type-guards';
import { ModuleWithOptions, ModuleDecorator, Module } from './decorators/module';
import { mergeArrays } from './utils/merge-arrays-options';
import { RootModule } from './decorators/root-module';
import { deepFreeze } from './utils/deep-freeze';
import { normalizeProviders } from './utils/ng-utils';

export abstract class Factory {
  protected throwProvidersCollisionError(moduleName: string, duplicates: any[]) {
    const names = duplicates.map((p) => p.name || p).join(', ');
    const provider = duplicates.length > 1 ? 'these providers' : 'this provider';
    throw new Error(
      `Exporting providers in ${moduleName} was failed: Collision was found for: ${names}. You should manually add ${provider} to ${moduleName}.`
    );
  }

  protected getModule(mod: Type<any> | ModuleWithOptions<any>) {
    return isModuleWithOptions(mod) ? mod.module : mod;
  }

  protected getModuleName(modOrObject: Type<any> | ModuleWithOptions<any>) {
    return isModuleWithOptions(modOrObject) ? modOrObject.module.name : modOrObject.name;
  }

  protected checkModuleMetadata(modMetadata: ModuleDecorator, modName: string, isRoot?: boolean) {
    const decoratorName = isRoot ? '@RootModule' : '@Module';

    if (!modMetadata) {
      throw new Error(`Module build failed: module "${modName}" does not have the "${decoratorName}()" decorator`);
    }
  }

  protected getRawModuleMetadata<T extends ModuleDecorator>(
    modOrObject: Type<any> | ModuleWithOptions<any>,
    isRoot?: boolean
  ) {
    let modMetadata: T;
    const typeGuard = isRoot ? isRootModule : (m: ModuleDecorator) => isModule(m) || isRootModule(m);
    const Metadata = isRoot ? RootModule : Module;

    if (isModuleWithOptions(modOrObject)) {
      const modWitOptions = modOrObject;
      modMetadata = reflector.annotations(modWitOptions.module).find(typeGuard) as T;
      const modName = this.getModuleName(modWitOptions.module);
      this.checkModuleMetadata(modMetadata, modName, isRoot);

      const metadata = new Metadata(modMetadata);
      metadata.providersPerApp = mergeArrays(modMetadata.providersPerApp, modWitOptions.providersPerApp);
      metadata.providersPerMod = mergeArrays(modMetadata.providersPerMod, modWitOptions.providersPerMod);
      metadata.providersPerReq = mergeArrays(modMetadata.providersPerReq, modWitOptions.providersPerReq);
      return deepFreeze(metadata);
    } else {
      return deepFreeze<T>(reflector.annotations(modOrObject).find(typeGuard));
    }
  }

  /**
   * Returns last provider if the provider has the duplicate.
   */
  protected getUniqProviders(providers: Provider[]) {
    const tokens = normalizeProviders(providers).map((np) => np.provide);
    const uniqProviders: Provider[] = [];

    tokens.forEach((currToken, currIndex) => {
      if (tokens.lastIndexOf(currToken) == currIndex) {
        uniqProviders.push(providers[currIndex]);
      }
    });

    return uniqProviders;
  }

  /**
   * Returns array of uniq tokens.
   *
   * If you have a replacement for some provider - you have a collision.
   */
  protected getTokensCollisions(uniqDuplTokens: any[], providers: Provider[]) {
    uniqDuplTokens = uniqDuplTokens || [];
    providers = providers || [];
    const duplProviders: Provider[] = [];

    normalizeProviders(providers)
      .map((np) => np.provide)
      .forEach((currToken, currIndex) => {
        if (uniqDuplTokens.includes(currToken)) {
          duplProviders.push(providers[currIndex]);
        }
      });

    const normDuplProviders = normalizeProviders(duplProviders);

    return uniqDuplTokens.filter((dulpToken) => {
      let prevProvider: Provider;

      for (let i = 0; i < normDuplProviders.length; i++) {
        if (normDuplProviders[i].provide !== dulpToken) {
          continue;
        }

        const currProvider = duplProviders[i];
        if (!prevProvider) {
          prevProvider = currProvider;
        }

        if (isProvider(prevProvider) && isProvider(currProvider)) {
          if (prevProvider.provide !== currProvider.provide || format(prevProvider) != format(currProvider)) {
            return true;
          }
          continue;
        }
        // TODO Remove it after a while.
        // else if (prevProvider !== currProvider) {
        //   return true;
        // }
        throw new TypeError(`Unrecognized providers found: ${prevProvider} and ${currProvider}`);
      }
    });
  }
}

{'fraction_non_alphanumeric': 0.12237423246795218, 'fraction_numerical': 0.00043089518474631047, 'mean_word_length': 3.5024248302618814, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4389820', 'n_tokens_mistral': 2734, 'n_tokens_neox': 2686, 'n_words': 613}