<commit_before>import Immutable from 'immutable'
import kebabCase from 'lodash/string/kebabCase'
import debug from 'debug'

import enhanceHanson from '../lib/enhance-hanson'
import pluralizeArea from '../lib/pluralize-area'

const migrationLog = debug('gobbldygook:data-migration:study')

export async function loadArea({name, type}) {
	const db = require('../lib/db')

	if (!name) {
		throw new Error(`loadArea(): 'name' must be provided`)
	}
	else if (!type) {
		throw new Error(`loadArea(): 'type' must be provided`)
	}

	const path = `${pluralizeArea(type)}/${kebabCase(name)}.yaml`

	let data = await db.stores.areas.get(path)

	if (!data) {
		throw new Error(`Could not load area ${path}`)
	}

	const enhanced = enhanceHanson(data, {topLevel: true})

	return enhanced
}

export const StudyRecord = Immutable.Record({
	id: '',
	type: '',
	name: '',
	revision: null,
	data: Promise.resolve({}),
})

export function expandOldType(type) {
	if (type === 'm') {
		return 'major'
	}
	else if (type === 'c') {
		return 'concentration'
	}
	else if (type === 'd') {
		return 'degree'
	}
	else if (type === 'e') {
		return 'emphasis'
	}
}

export function expandOldName(name) {
	if (name === 'csci') {
		return 'Computer Science'
	}
	else if (name === 'math') {
		return 'Math'
	}
	else if (name === 'phys') {
		return 'Physics'
	}
	else if (name === 'asian') {
		return 'Asian Studies'
	}
	else if (name === 'stat') {
		return 'Statistics'
	}
	else if (name === 'japan') {
		return 'Japan Studies'
	}
	else if (name === 'ba') {
		return 'Bachelor of Arts'
	}
}

export function expandOldRevisionYear(revisionYear) {
	return `${revisionYear}-${parseInt(String(revisionYear).slice(2, 4)) + 1}`
}

export function migrateFromOldSave({id, revisionYear}) {
	const [t, n] = id.split('-')
	const type = expandOldType(t)
	const name = expandOldName(n)
	const revision = expandOldRevisionYear(revisionYear)
	return {name, type, revision}
}

export default class Study extends StudyRecord {
	constructor(args) {
		let {name, type, revision} = args

		// migrate from older area save style
		if ('id' in args) {
			migrationLog(`migrating ${args.id}`);
			({name, type, revision} = migrateFromOldSave(args))
		}

		const id = `${kebabCase(name)}-${type}?rev=${revision}`
		const data = loadArea({name, type, revision})
			.catch(err => ({_error: err.message}))

		super({
			type,
			name,
			revision,
			id,
			data,
		})
	}

	toJSON() {
		return {
			type: this.type,
			name: this.name,
			revision: this.revision,
		}
	}
}
<commit_msg>be consistent with regards to accessing idb stores<commit_after>import Immutable from 'immutable'
import kebabCase from 'lodash/string/kebabCase'
import debug from 'debug'

import enhanceHanson from '../lib/enhance-hanson'
import pluralizeArea from '../lib/pluralize-area'

const migrationLog = debug('gobbldygook:data-migration:study')

export async function loadArea({name, type}) {
	const db = require('../lib/db')

	if (!name) {
		throw new Error(`loadArea(): 'name' must be provided`)
	}
	else if (!type) {
		throw new Error(`loadArea(): 'type' must be provided`)
	}

	const path = `${pluralizeArea(type)}/${kebabCase(name)}.yaml`

	let data = await db.store('areas').get(path)

	if (!data) {
		throw new Error(`Could not load area ${path}`)
	}

	const enhanced = enhanceHanson(data, {topLevel: true})

	return enhanced
}

export const StudyRecord = Immutable.Record({
	id: '',
	type: '',
	name: '',
	revision: null,
	data: Promise.resolve({}),
})

export function expandOldType(type) {
	if (type === 'm') {
		return 'major'
	}
	else if (type === 'c') {
		return 'concentration'
	}
	else if (type === 'd') {
		return 'degree'
	}
	else if (type === 'e') {
		return 'emphasis'
	}
}

export function expandOldName(name) {
	if (name === 'csci') {
		return 'Computer Science'
	}
	else if (name === 'math') {
		return 'Math'
	}
	else if (name === 'phys') {
		return 'Physics'
	}
	else if (name === 'asian') {
		return 'Asian Studies'
	}
	else if (name === 'stat') {
		return 'Statistics'
	}
	else if (name === 'japan') {
		return 'Japan Studies'
	}
	else if (name === 'ba') {
		return 'Bachelor of Arts'
	}
}

export function expandOldRevisionYear(revisionYear) {
	return `${revisionYear}-${parseInt(String(revisionYear).slice(2, 4)) + 1}`
}

export function migrateFromOldSave({id, revisionYear}) {
	const [t, n] = id.split('-')
	const type = expandOldType(t)
	const name = expandOldName(n)
	const revision = expandOldRevisionYear(revisionYear)
	return {name, type, revision}
}

export default class Study extends StudyRecord {
	constructor(args) {
		let {name, type, revision} = args

		// migrate from older area save style
		if ('id' in args) {
			migrationLog(`migrating ${args.id}`);
			({name, type, revision} = migrateFromOldSave(args))
		}

		const id = `${kebabCase(name)}-${type}?rev=${revision}`
		const data = loadArea({name, type, revision})
			.catch(err => ({_error: err.message}))

		super({
			type,
			name,
			revision,
			id,
			data,
		})
	}

	toJSON() {
		return {
			type: this.type,
			name: this.name,
			revision: this.revision,
		}
	}
}

{'fraction_non_alphanumeric': 0.16604440951070937, 'fraction_numerical': 0.0011790135586559245, 'mean_word_length': 3.9950932286555445, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6231996', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1795, 'n_words': 487}