Skip to content

Commit

Permalink
refactor: better options merging strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
arianrhodsandlot committed Mar 5, 2024
1 parent dd2ee67 commit 2479fbc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
21 changes: 2 additions & 19 deletions src/nostalgist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,9 @@ export class Nostalgist {
private constructor(options: NostalgistLaunchOptions) {
const globalOptions: Partial<NostalgistLaunchOptions> = { ...Nostalgist.globalOptions }
const localOptions = { ...options }

const element = globalOptions.element || localOptions.element
const rom = globalOptions.rom || localOptions.rom
const bios = globalOptions.bios || localOptions.bios
const signal = globalOptions.signal || localOptions.signal

const mergedOptions = { rom, bios, element, signal }

delete globalOptions.rom
delete globalOptions.bios
delete globalOptions.element
delete globalOptions.signal

delete localOptions.rom
delete localOptions.bios
delete localOptions.element
delete localOptions.signal

const mergedOptions = {} as NostalgistOptions
merge(mergedOptions, globalOptions, localOptions)
this.options = mergedOptions as NostalgistOptions
this.options = mergedOptions
}

/**
Expand Down
18 changes: 7 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,10 @@ function isNil(obj: unknown) {
return obj === undefined || obj === null
}

function isPrimitive(obj: unknown) {
function isPlainObject(obj: any) {
if (isNil(obj)) {
return true
return false
}
const type = typeof obj
const primitevTypes = ['string', 'number', 'boolean', 'bigint', 'symbol', 'function']
return primitevTypes.includes(type)
}

function isPlainObject(obj: any) {
const { constructor } = obj
return constructor === Object || !constructor
}
Expand All @@ -122,13 +116,15 @@ function mergeSourceToTarget(target: any, source: any) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
const targetValue = target[key]
const sourceValue = source[key]
if (isPrimitive(sourceValue) || !isPlainObject(sourceValue)) {
if (isNil(targetValue)) {
target[key] = sourceValue
} else if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = [...targetValue, ...sourceValue]
} else if (isPlainObject(targetValue) && isPlainObject(sourceValue)) {
target[key] = isPlainObject(targetValue) ? target[key] : {}
mergeSourceToTarget(target[key], sourceValue)
} else {
target[key] = isPrimitive(targetValue) || !isPlainObject(targetValue) ? {} : target[key]
merge(target[key], sourceValue)
target[key] = sourceValue
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/nostalgist.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function resolveBios(bios: any) {
return `https://buildbot.libretro.com/assets/system/${bios}`
}

describe('nostalgist', () => {
describe.concurrent('nostalgist', () => {
beforeEach(() => {
Nostalgist.configure({ runEmulatorManually: true })
})
Expand Down Expand Up @@ -225,6 +225,7 @@ describe('nostalgist', () => {
expect(options.retroarchConfig).toMatchObject({
menu_driver: 'rgui',
input_menu_toggle: 'nul',
input_max_users: 4,
input_audio_mute: 'b',
})
})
Expand Down

0 comments on commit 2479fbc

Please sign in to comment.