<commit_before>import {Serializable} from './types';

declare abstract class Reflect {
    static getMetadata(metadataKey: string, target: any, targetKey: string | symbol): any;
}

export const MISSING_REFLECT_CONF_MSG = 'Are you sure, that you have both "experimentalDecorators"'
    + ' and "emitDecoratorMetadata" in your tsconfig.json?';

/**
 * Determines whether the specified type is a type that can be passed on "as-is" into
 * `JSON.stringify`.
 * Values of these types don't need special conversion.
 * @param type The constructor of the type (wrapper constructor for primitive types, e.g. `Number`
 * for `number`).
 */
export function isDirectlySerializableNativeType(type: Function): boolean {
    return [Date, Number, String, Boolean].indexOf(type as any) !== -1;
}

export function isDirectlyDeserializableNativeType(type: Function): boolean {
    return [Number, String, Boolean].indexOf(type as any) !== -1;
}

export function isTypeTypedArray(type: Function): boolean {
    return [
        Float32Array,
        Float64Array,
        Int8Array,
        Uint8Array,
        Uint8ClampedArray,
        Int16Array,
        Uint16Array,
        Int32Array,
        Uint32Array,
    ].indexOf(type as any) !== -1;
}

export function isObject(value: any): value is Object {
    return typeof value === 'object';
}

export function shouldOmitParseString(jsonStr: string, expectedType: Function): boolean {
    const expectsTypesSerializedAsStrings = expectedType === String
        || expectedType === ArrayBuffer
        || expectedType === DataView;

    const hasQuotes = jsonStr.length >= 2
        && jsonStr[0] === '"'
        && jsonStr[jsonStr.length - 1] === '"';
    const isInteger = /^\d+$/.test(jsonStr.trim());

    return (expectsTypesSerializedAsStrings && !hasQuotes)
        || ((!hasQuotes && !isInteger) && expectedType === Date);
}

export function parseToJSObject<T>(json: any, expectedType: Serializable<T>): Object {
    if (typeof json !== 'string' || shouldOmitParseString(json, expectedType)) {
        return json;
    }
    return JSON.parse(json);
}

/**
 * Determines if 'A' is a sub-type of 'B' (or if 'A' equals 'B').
 * @param A The supposed derived type.
 * @param B The supposed base type.
 */
export function isSubtypeOf(A: Function, B: Function) {
    return A === B || A.prototype instanceof B;
}

export function logError(message?: any, ...optionalParams: Array<any>) {
    if (typeof console as any === 'object' && typeof console.error as any === 'function') {
        console.error(message, ...optionalParams);
    } else if (typeof console as any === 'object' && typeof console.log as any === 'function') {
        console.log(`ERROR: ${message}`, ...optionalParams);
    }
}

export function logMessage(message?: any, ...optionalParams: Array<any>) {
    if (typeof console as any === 'object' && typeof console.log as any === 'function') {
        console.log(message, ...optionalParams);
    }<commit_msg>Fix shouldOmitParseString handling of numbers for dates<commit_after>import {Serializable} from './types';

declare abstract class Reflect {
    static getMetadata(metadataKey: string, target: any, targetKey: string | symbol): any;
}

export const MISSING_REFLECT_CONF_MSG = 'Are you sure, that you have both "experimentalDecorators"'
    + ' and "emitDecoratorMetadata" in your tsconfig.json?';

/**
 * Determines whether the specified type is a type that can be passed on "as-is" into
 * `JSON.stringify`.
 * Values of these types don't need special conversion.
 * @param type The constructor of the type (wrapper constructor for primitive types, e.g. `Number`
 * for `number`).
 */
export function isDirectlySerializableNativeType(type: Function): boolean {
    return [Date, Number, String, Boolean].indexOf(type as any) !== -1;
}

export function isDirectlyDeserializableNativeType(type: Function): boolean {
    return [Number, String, Boolean].indexOf(type as any) !== -1;
}

export function isTypeTypedArray(type: Function): boolean {
    return [
        Float32Array,
        Float64Array,
        Int8Array,
        Uint8Array,
        Uint8ClampedArray,
        Int16Array,
        Uint16Array,
        Int32Array,
        Uint32Array,
    ].indexOf(type as any) !== -1;
}

export function isObject(value: any): value is Object {
    return typeof value === 'object';
}

export function shouldOmitParseString(jsonStr: string, expectedType: Function): boolean {
    const expectsTypesSerializedAsStrings = expectedType === String
        || expectedType === ArrayBuffer
        || expectedType === DataView;

    const hasQuotes = jsonStr.length >= 2
        && jsonStr[0] === '"'
        && jsonStr[jsonStr.length - 1] === '"';

    if (expectedType === Date) {
        // Date can both have strings and numbers as input
        const isNumber = !isNaN(Number(jsonStr.trim()));
        return !hasQuotes && !isNumber;
    }

    return expectsTypesSerializedAsStrings && !hasQuotes;
}

export function parseToJSObject<T>(json: any, expectedType: Serializable<T>): Object {
    if (typeof json !== 'string' || shouldOmitParseString(json, expectedType)) {
        return json;
    }
    return JSON.parse(json);
}

/**
 * Determines if 'A' is a sub-type of 'B' (or if 'A' equals 'B').
 * @param A The supposed derived type.
 * @param B The supposed base type.
 */
export function isSubtypeOf(A: Function, B: Function) {
    return A === B || A.prototype instanceof B;
}

export function logError(message?: any, ...optionalParams: Array<any>) {
    if (typeof console as any === 'object' && typeof console.error as any === 'function') {
        console.error(message, ...optionalParams);
    } else if (typeof console as any === 'object' && typeof console.log as any === 'function') {
        console.log(`ERROR: ${message}`, ...optionalParams);
    }
}

export function logMessage(message?: any, ...optionalParams: Array<any>) {
    if (typeof console as any === 'object' && typeof console.log as any === 'function') {
        console.log(message, ...optionalParams);
    }
{'fraction_non_alphanumeric': 0.13113940797089466, 'fraction_numerical': 0.006945592855961634, 'mean_word_length': 3.5201793721973096, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '6854114', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1704, 'n_words': 551}