Repository: facebook/jest
File: packages/jest-create-cache-key-function/src/index.ts (TypeScript)

Code before:
    // Jest 27 passes a single options bag which contains `configString` rather than as a separate argument.
    // We can hide that API difference, though, so this module is usable for both jest@<27 and jest@>=27
    const inferredOptions = options || configString;
    const {config, instrument} = inferredOptions;

    return createHash('sha1')
      .update(globalCacheKey)
      .update('\0', 'utf8')
      .update(sourceText)
      .update('\0', 'utf8')
      .update(config.rootDir ? relative(config.rootDir, sourcePath) : '')
      .update('\0', 'utf8')
      .update(instrument ? 'instrument' : '')
      .digest('hex')
      .substring(0, 32);
  };
}

/**
 * Provides a function that computes the cache key given a collection of files and values.
 * This limits the output with a provided length.
 * @param files Array of absolute paths to files whose code should be accounted for when generating cache key
 * @param values Array of string values that should be accounted for when generating cache key
 * @param length length of the resulting key defaults to 16 on win32 and 32 elsewhere
 * @returns a function that is used to create the cache key.
 */
export default function createCacheKey(
  files: Array<string> = [],
  values: Array<string> = [],
  length = process.platform === 'win32' ? 16 : 32,
): GetCacheKeyFunction {
  return getCacheKeyFunction(getGlobalCacheKey(files, values, length));
}


Review comment (performance):
Would be nice to follow TSDoc standard:

```suggestion
/**
  * Returns a function that can be used to generate cache keys based on source code of provided files and provided values.
  * 
  * @param files - Array of absolute paths to files whose code should be accounted for when generating cache key
  * @param values - Array of string values that should be accounted for when generating cache key
  * @param length - Length of the resulting key. The default is `32`, or `16` on Windows.
  * @returns A function that can be used to generate cache keys.
  */
```

Code after:
  length: number,
): GetCacheKeyFunction {
  return (sourceText, sourcePath, configString, options) => {
    // Jest 27 passes a single options bag which contains `configString` rather than as a separate argument.
    // We can hide that API difference, though, so this module is usable for both jest@<27 and jest@>=27
    const inferredOptions = options || configString;
    const {config, instrument} = inferredOptions;

    return createHash('sha1')
      .update(globalCacheKey)
      .update('\0', 'utf8')
      .update(sourceText)
      .update('\0', 'utf8')
      .update(config.rootDir ? relative(config.rootDir, sourcePath) : '')
      .update('\0', 'utf8')
      .update(instrument ? 'instrument' : '')
      .digest('hex')
      .substring(0, length);
  };
}

/**
 * Returns a function that can be used to generate cache keys based on source code of provided files and provided values.
 *
 * @param files - Array of absolute paths to files whose code should be accounted for when generating cache key
 * @param values - Array of string values that should be accounted for when generating cache key
 * @param length - Length of the resulting key. The default is `32`, or `16` on Windows.
 * @returns A function that can be used to generate cache keys.
 */
export default function createCacheKey(
  files: Array<string> = [],
  values: Array<string> = [],
  length = process.platform === 'win32' ? 16 : 32,
): GetCacheKeyFunction {
  return getCacheKeyFunction(getGlobalCacheKey(files, values, length), length);
}