<commit_before>import fs from 'fs-extra';
import moment from 'moment';

const maxItemsInState = 100;

export default class State {
    constructor(filePath) {
        this.filePath = filePath;
        this.data = {
            pageToken: null,
            docs: {}
        };
    }

    getPageToken() {
        return this.data.pageToken;
    }

    setPageToken(token) {
        this.data.pageToken = token;
    }

    setFile(fileId, data) {
        this.data.docs[fileId] = data;
    }

    getFile(fileId) {
        return this.data.docs[fileId];
    }

    save(stateUpdate) {
        this.data = { ...this.data, ...stateUpdate };
        const { docs } = this.data;

        const ids = Object.keys(docs);

        if (ids.length > maxItemsInState) {
            ids
                .filter(id => docs[id].file && docs[id].file.modifiedDate)
                .sort(
                    (a, b) =>
                        moment(docs[b].file.modifiedDate).valueOf() -
                        moment(docs[a].file.modifiedDate).valueOf()
                )
                .slice(maxItemsInState)
                .forEach(id => delete docs[id]);
        }

        return fs.outputJson(this.filePath, this.data).then(() => this);
    }

    read() {
        return fs
            .readFile(this.filePath, 'utf-8')
            .catch(err => null)
            .then(JSON.parse)
            .then(data => (this.data = data || this.data))
            .then(() => this.data);
    }
}<commit_msg>fix bug when initializing<commit_after>import fs from 'fs-extra';
import moment from 'moment';

const maxItemsInState = 100;

const emptyState = () => ({
    pageToken: null,
    docs: {},
});

export default class State {
    constructor(filePath) {
        this.filePath = filePath;
        this.data = emptyState();
    }

    getPageToken() {
        return this.data.pageToken;
    }

    setPageToken(token) {
        this.data.pageToken = token;
    }

    setFile(fileId, data) {
        this.data.docs[fileId] = data;
    }

    getFile(fileId) {
        return this.data.docs[fileId];
    }

    save(stateUpdate) {
        this.data = { ...this.data, ...stateUpdate };
        const { docs } = this.data;

        const ids = Object.keys(docs);

        if (ids.length > maxItemsInState) {
            ids.filter(id => docs[id].file && docs[id].file.modifiedDate)
                .sort(
                    (a, b) =>
                        moment(docs[b].file.modifiedDate).valueOf() -
                        moment(docs[a].file.modifiedDate).valueOf()
                )
                .slice(maxItemsInState)
                .forEach(id => delete docs[id]);
        }

        return fs.outputJson(this.filePath, this.data).then(() => this);
    }

    read() {
        return fs
            .readFile(this.filePath, 'utf-8')
            .then(JSON.parse)
            .catch(err => null)
            .then(data => (this.data = data || this.data))
            .then(() => this.data);
    }
}
{'fraction_non_alphanumeric': 0.14185580774365822, 'fraction_numerical': 0.0026702269692923898, 'mean_word_length': 1.5703259005145798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '6665371', 'n_tokens_mistral': 934, 'n_tokens_neox': 883, 'n_words': 163}