Browse Source

Make function prototypes rigid

Convert interfaces to types for service functions
master
TheoryOfNekomata 4 years ago
parent
commit
d9eb01b7ea
10 changed files with 14 additions and 42 deletions
  1. +1
    -3
      src/services/generateKeys.ts
  2. +1
    -3
      src/services/getFractionalOctaveCount.ts
  3. +3
    -9
      src/services/getKeyLeft.ts
  4. +1
    -3
      src/services/getKeyOctave.ts
  5. +3
    -9
      src/services/getKeyWidth.ts
  6. +1
    -3
      src/services/getKeyXOffset.ts
  7. +1
    -3
      src/services/getOctaveCompleteness.ts
  8. +1
    -3
      src/services/getOctaveCount.ts
  9. +1
    -3
      src/services/groupKeysIntoOctaves.ts
  10. +1
    -3
      src/services/isNaturalKey.ts

+ 1
- 3
src/services/generateKeys.ts View File

@@ -1,6 +1,4 @@
interface GenerateKeys {
(startKey: number, endKey: number): number[]
}
type GenerateKeys = (startKey: number, endKey: number) => number[]

const generateKeys: GenerateKeys = (startKey, endKey) =>
Array(endKey - startKey + 1)


+ 1
- 3
src/services/getFractionalOctaveCount.ts View File

@@ -6,9 +6,7 @@ import * as caches from './caches'

const getOctaveCompleteness = mem(getOctaveCompletenessUnmemoized, { cache: caches.getOctaveCompleteness })

interface GetFractionalOctaveCount {
(startKey: number, endKey: number): number
}
type GetFractionalOctaveCount = (startKey: number, endKey: number) => number

const getFractionalOctaveCount: GetFractionalOctaveCount = (startKey, endKey) => {
const dummyKeys = generateKeys(startKey, endKey)


+ 3
- 9
src/services/getKeyLeft.ts View File

@@ -10,15 +10,9 @@ const getOctaveCount = mem(getOctaveCountUnmemoized, { cacheKey: (args) => args.
const getFractionalOctaveCount = mem(getFractionalOctaveCountUnmemoized, { cacheKey: (args) => args.join(':') })
const getKeyOctave = mem(getKeyOctaveUnmemoized, { cache: caches.getKeyOctave })

export interface GetKeyLeft {
(k: number): number
}

interface GetKeyLeftDecorator {
(startKey: number, endKey: number): GetKeyLeft
}
type GetKeyLeft = (startKey: number, endKey: number) => (k: number) => number

const getKeyLeftDecorator: GetKeyLeftDecorator = (startKey, endKey): GetKeyLeft => (k) => {
const getKeyLeft: GetKeyLeft = (startKey, endKey) => (k) => {
const fractionalOctaveCount = getFractionalOctaveCount(startKey, endKey)
const octaveCount = getOctaveCount(startKey, endKey)
const startOctave = getKeyOctave(startKey)
@@ -29,4 +23,4 @@ const getKeyLeftDecorator: GetKeyLeftDecorator = (startKey, endKey): GetKeyLeft
return theKeyOffset - firstKeyOffset
}

export default getKeyLeftDecorator
export default getKeyLeft

+ 1
- 3
src/services/getKeyOctave.ts View File

@@ -1,6 +1,4 @@
interface GetKeyOctave {
(k: number): number
}
type GetKeyOctave = (k: number) => number

const getKeyOctave: GetKeyOctave = (k) => Math.floor(k / 12)



+ 3
- 9
src/services/getKeyWidth.ts View File

@@ -5,13 +5,7 @@ import getFractionalOctaveCountUnmemoized from './getFractionalOctaveCount'
import { ACCIDENTAL_KEY_TO_NATURAL_KEY_WIDTH_RATIO } from './constants'
import * as caches from './caches'

export interface GetKeyWidth {
(k: number): number
}

interface GetKeyWidthDecorator {
(startKey: number, endKey: number): GetKeyWidth
}
type GetKeyWidthDecorator = (startKey: number, endKey: number) => (k: number) => number

const isNaturalKey = mem(isNaturalKeyUnmemoized, { cache: caches.isNaturalKey })
const getFractionalOctaveCount = mem(getFractionalOctaveCountUnmemoized, {
@@ -19,11 +13,11 @@ const getFractionalOctaveCount = mem(getFractionalOctaveCountUnmemoized, {
})
const getOctaveCount = mem(getOctaveCountUnmemoized, { cacheKey: (args) => args.join(':') })

const getKeyWidthDecorator: GetKeyWidthDecorator = (startKey, endKey) => (k) => {
const getKeyWidth: GetKeyWidthDecorator = (startKey, endKey) => (k) => {
const fractionalOctaveCount = getFractionalOctaveCount(startKey, endKey)
const octaveCount = getOctaveCount(startKey, endKey)
const naturalKeyWidth = (100 * (octaveCount / fractionalOctaveCount)) / (octaveCount * 7)
return isNaturalKey(k) ? naturalKeyWidth : naturalKeyWidth * ACCIDENTAL_KEY_TO_NATURAL_KEY_WIDTH_RATIO
}

export default getKeyWidthDecorator
export default getKeyWidth

+ 1
- 3
src/services/getKeyXOffset.ts View File

@@ -1,8 +1,6 @@
import { KEY_OFFSETS } from './constants'

interface GetKeyXOffset {
(k: number): number
}
type GetKeyXOffset = (k: number) => number

const getKeyXOffset: GetKeyXOffset = (k) => {
return KEY_OFFSETS[k % 12]


+ 1
- 3
src/services/getOctaveCompleteness.ts View File

@@ -6,9 +6,7 @@ import isNaturalKeyUnmemoized from './isNaturalKey'
const getKeyXOffset = mem(getKeyXOffsetUnmemoized, { cache: caches.getKeyXOffset })
const isNaturalKey = mem(isNaturalKeyUnmemoized, { cache: caches.isNaturalKey })

interface GetOctaveCompleteness {
(firstKey: number, lastKey: number): number
}
type GetOctaveCompleteness = (firstKey: number, lastKey: number) => number

// expect firstKey and lastKey within the same octave
const getOctaveCompleteness: GetOctaveCompleteness = (firstKey, lastKey) =>


+ 1
- 3
src/services/getOctaveCount.ts View File

@@ -4,9 +4,7 @@ import * as caches from './caches'

const getKeyOctave = mem(getKeyOctaveUnmemoized, { cache: caches.getKeyOctave })

interface GetOctaveCount {
(startKey: number, endKey: number): number
}
type GetOctaveCount = (startKey: number, endKey: number) => number

const getOctaveCount: GetOctaveCount = (startKey, endKey) => getKeyOctave(endKey) - getKeyOctave(startKey) + 1



+ 1
- 3
src/services/groupKeysIntoOctaves.ts View File

@@ -1,6 +1,4 @@
interface GroupKeysIntoOctaves {
(dummyKeys: number[]): Record<number, number[]>
}
type GroupKeysIntoOctaves = (dummyKeys: number[]) => Record<number, number[]>

const groupKeysIntoOctaves: GroupKeysIntoOctaves = (dummyKeys) =>
dummyKeys


+ 1
- 3
src/services/isNaturalKey.ts View File

@@ -1,8 +1,6 @@
const NATURAL_KEYS = [0, 2, 4, 5, 7, 9, 11]

interface IsNaturalKey {
(k: number): boolean
}
type IsNaturalKey = (k: number) => boolean

const isNaturalKey: IsNaturalKey = (k: number): boolean => {
const type = typeof (k as unknown)


Loading…
Cancel
Save