Skip to content

Commit

Permalink
Convert FP and indices to TypeScript (date-fns#2525)
Browse files Browse the repository at this point in the history
  • Loading branch information
fturmel authored Dec 21, 2021
1 parent e0fc48a commit c7ef5f0
Show file tree
Hide file tree
Showing 251 changed files with 1,343 additions and 1,797 deletions.
15 changes: 11 additions & 4 deletions scripts/_lib/listFPFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ const fs = require('fs')

module.exports = listFPFns

const ignoredFiles = ['index.js', 'test.js', 'index.js.flow', 'package.json']
const ignoredFiles = [
'index.js',
'index.ts',
'test.js',
'test.ts',
'index.js.flow',
'package.json',
]

function listFPFns() {
const files = fs.readdirSync(path.join(process.cwd(), 'src', 'fp'))
return files
.filter(file => /^[^._]/.test(file) && !ignoredFiles.includes(file))
.map(file => ({
.filter((file) => /^[^._]/.test(file) && !ignoredFiles.includes(file))
.map((file) => ({
name: file,
path: `./${file}`,
fullPath: `./src/fp/${file}/index.js`
fullPath: `./src/fp/${file}/index.js`,
}))
}
10 changes: 6 additions & 4 deletions scripts/_lib/listFns.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ const ignoredFiles = [
'fp',
'constants',
'index.js',
'index.ts',
'test.js',
'test.ts',
'index.js.flow',
'package.json',
'types.ts'
'types.ts',
]

async function listFns() {
Expand All @@ -25,13 +27,13 @@ async function listFns() {

return Promise.all(
files
.filter(file => /^[^._]/.test(file) && !ignoredFiles.includes(file))
.map(async file => {
.filter((file) => /^[^._]/.test(file) && !ignoredFiles.includes(file))
.map(async (file) => {
const isTs = await exists(path.join(srcPath, file, 'index.ts'))
return {
name: file,
path: `./${file}`,
fullPath: `./src/${file}/index.${isTs ? 'ts' : 'js'}`
fullPath: `./src/${file}/index.${isTs ? 'ts' : 'js'}`,
}
})
)
Expand Down
2 changes: 2 additions & 0 deletions scripts/_lib/listLocales.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const readDir = promisify(fs.readdir)

const ignoredFiles = [
'index.js',
'index.ts',
'test.js',
'test.ts',
'index.js.flow',
'package.json',
'types.ts',
Expand Down
6 changes: 2 additions & 4 deletions scripts/build/fp.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ function getFPFn(resultFnName, initialFnName, arity) {
.concat(`import fn from '../../${initialFnName}/index'`)
.concat(`import convertToFP from '../_lib/convertToFP/index'`)
.concat('')
.concat(`var ${resultFnName} = convertToFP(fn, ${arity})`)
.concat('')
.concat(`export default ${resultFnName}`)
.concat(`export default convertToFP(fn, ${arity})`)
.concat('')
.join('\n')
}
Expand All @@ -43,7 +41,7 @@ function buildFPFn({ title, generatedFrom, args: { length } }) {
if (!fs.existsSync(fpFnDir)) {
fs.mkdirSync(fpFnDir)
}
fs.writeFileSync(`${fpFnDir}/index.js`, prettier(fpFnLines))
fs.writeFileSync(`${fpFnDir}/index.ts`, prettier(fpFnLines))
}

function buildFP(fns) {
Expand Down
7 changes: 4 additions & 3 deletions scripts/build/indices.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
const fs = require('fs')
const path = require('path')
const prettier = require('./_lib/prettier')

const listFns = require('../_lib/listFns')
const listFPFns = require('../_lib/listFPFns')
const listLocales = require('../_lib/listLocales')
Expand All @@ -25,9 +26,9 @@ Promise.all([listFns(), listLocales()]).then(([fns, allLocales]) => {
({ code }) => !outdatedLocales.includes(code)
)

writeFile('src/index.js', generateIndex(fns, false, true))
writeFile('src/fp/index.js', generateIndex(fpFns, true, true))
writeFile('src/locale/index.js', generateIndex(locales, false, false))
writeFile('src/index.ts', generateIndex(fns, false, true))
writeFile('src/fp/index.ts', generateIndex(fpFns, true, true))
writeFile('src/locale/index.ts', generateIndex(locales, false, false))
})

function writeFile(relativePath, content) {
Expand Down
12 changes: 0 additions & 12 deletions src/fp/_lib/convertToFP/index.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/fp/_lib/convertToFP/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default function convertToFP(
fn: Function,
arity: number,
a: any[] = []
) {
if (a.length >= arity) {
return fn.apply(null, a.slice(0, arity).reverse())
}

return function (...args: any[]) {
return convertToFP(fn, arity, a.concat(args))
}
}
25 changes: 14 additions & 11 deletions src/fp/_lib/convertToFP/test.js → src/fp/_lib/convertToFP/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,67 @@ import assert from 'power-assert'
import convertToFP from '.'

describe('convertToFP', function () {
function fn(a, b, c) {
return 'a b c'.replace('a', a).replace('b', b).replace('c', c)
function fn(a: unknown, b: unknown, c: unknown) {
return 'a b c'
.replace('a', String(a))
.replace('b', String(b))
.replace('c', String(c))
}

describe('arity of converted function === arity of initial function', function () {
it('allows arguments to be curried (and reverses their order)', function () {
var fpFn = convertToFP(fn, 3)
const fpFn = convertToFP(fn, 3)
assert(fpFn(3)(2)(1) === '1 2 3')
})

it('allows to group arguments', function () {
var fpFn = convertToFP(fn, 3)
const fpFn = convertToFP(fn, 3)
assert(fpFn(3, 2)(1) === '1 2 3')
assert(fpFn(3)(2, 1) === '1 2 3')
})

it('allows the function to be called with all arguments in the reversed order', function () {
var fpFn = convertToFP(fn, 3)
const fpFn = convertToFP(fn, 3)
assert(fpFn(3, 2, 1) === '1 2 3')
})

it('ignores calls without curried arguments', function () {
var fpFn = convertToFP(fn, 3)
const fpFn = convertToFP(fn, 3)
assert(fpFn()()(3, 2)()()(1) === '1 2 3')
})

it('ignores extra curried arguments in the last group', function () {
var fpFn = convertToFP(fn, 3)
const fpFn = convertToFP(fn, 3)
assert(fpFn(3, 2, 1, 0, -1, -2) === '1 2 3')
assert(fpFn(3)(2)(1, 0, -1, -2) === '1 2 3')
})
})

describe('arity of converted function < arity of initial function', function () {
it('calls the initial function with a short list of arguments', function () {
var fpFn = convertToFP(fn, 2)
const fpFn = convertToFP(fn, 2)
assert(fpFn(2)(1) === '1 2 undefined')
assert(fpFn(2, 1) === '1 2 undefined')
})

it('ignores extra curried arguments in the last group', function () {
var fpFn = convertToFP(fn, 2)
const fpFn = convertToFP(fn, 2)
assert(fpFn(3)(2, 1) === '2 3 undefined')
assert(fpFn(3, 2, 1) === '2 3 undefined')
})
})

describe('arity of converted function > arity of initial function', function () {
it('works, but ignores the extra arguments', function () {
var fpFn = convertToFP(fn, 4)
const fpFn = convertToFP(fn, 4)
assert(fpFn(4)(3)(2)(1) === '1 2 3')
assert(fpFn(4, 3, 2, 1) === '1 2 3')
})
})

describe('arity of converted function === 0', function () {
it('returns the constant instead of function', function () {
var result = convertToFP(fn, 0)
const result = convertToFP(fn, 0)
assert(result === 'undefined undefined undefined')
})
})
Expand Down
4 changes: 1 addition & 3 deletions src/fp/add/index.js → src/fp/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../add/index'
import convertToFP from '../_lib/convertToFP/index'

var add = convertToFP(fn, 2)

export default add
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addBusinessDays/index'
import convertToFP from '../_lib/convertToFP/index'

var addBusinessDays = convertToFP(fn, 2)

export default addBusinessDays
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addDays/index.js → src/fp/addDays/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addDays/index'
import convertToFP from '../_lib/convertToFP/index'

var addDays = convertToFP(fn, 2)

export default addDays
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addHours/index.js → src/fp/addHours/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addHours/index'
import convertToFP from '../_lib/convertToFP/index'

var addHours = convertToFP(fn, 2)

export default addHours
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addISOWeekYears/index'
import convertToFP from '../_lib/convertToFP/index'

var addISOWeekYears = convertToFP(fn, 2)

export default addISOWeekYears
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addMilliseconds/index'
import convertToFP from '../_lib/convertToFP/index'

var addMilliseconds = convertToFP(fn, 2)

export default addMilliseconds
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addMinutes/index.js → src/fp/addMinutes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addMinutes/index'
import convertToFP from '../_lib/convertToFP/index'

var addMinutes = convertToFP(fn, 2)

export default addMinutes
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addMonths/index.js → src/fp/addMonths/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addMonths/index'
import convertToFP from '../_lib/convertToFP/index'

var addMonths = convertToFP(fn, 2)

export default addMonths
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addQuarters/index.js → src/fp/addQuarters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addQuarters/index'
import convertToFP from '../_lib/convertToFP/index'

var addQuarters = convertToFP(fn, 2)

export default addQuarters
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addSeconds/index.js → src/fp/addSeconds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addSeconds/index'
import convertToFP from '../_lib/convertToFP/index'

var addSeconds = convertToFP(fn, 2)

export default addSeconds
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addWeeks/index.js → src/fp/addWeeks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addWeeks/index'
import convertToFP from '../_lib/convertToFP/index'

var addWeeks = convertToFP(fn, 2)

export default addWeeks
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/addYears/index.js → src/fp/addYears/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../addYears/index'
import convertToFP from '../_lib/convertToFP/index'

var addYears = convertToFP(fn, 2)

export default addYears
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../areIntervalsOverlapping/index'
import convertToFP from '../_lib/convertToFP/index'

var areIntervalsOverlapping = convertToFP(fn, 2)

export default areIntervalsOverlapping
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../areIntervalsOverlapping/index'
import convertToFP from '../_lib/convertToFP/index'

var areIntervalsOverlappingWithOptions = convertToFP(fn, 3)

export default areIntervalsOverlappingWithOptions
export default convertToFP(fn, 3)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../closestIndexTo/index'
import convertToFP from '../_lib/convertToFP/index'

var closestIndexTo = convertToFP(fn, 2)

export default closestIndexTo
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/closestTo/index.js → src/fp/closestTo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../closestTo/index'
import convertToFP from '../_lib/convertToFP/index'

var closestTo = convertToFP(fn, 2)

export default closestTo
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/compareAsc/index.js → src/fp/compareAsc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../compareAsc/index'
import convertToFP from '../_lib/convertToFP/index'

var compareAsc = convertToFP(fn, 2)

export default compareAsc
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/compareDesc/index.js → src/fp/compareDesc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../compareDesc/index'
import convertToFP from '../_lib/convertToFP/index'

var compareDesc = convertToFP(fn, 2)

export default compareDesc
export default convertToFP(fn, 2)
4 changes: 1 addition & 3 deletions src/fp/daysToWeeks/index.js → src/fp/daysToWeeks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../daysToWeeks/index'
import convertToFP from '../_lib/convertToFP/index'

var daysToWeeks = convertToFP(fn, 1)

export default daysToWeeks
export default convertToFP(fn, 1)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../differenceInBusinessDays/index'
import convertToFP from '../_lib/convertToFP/index'

var differenceInBusinessDays = convertToFP(fn, 2)

export default differenceInBusinessDays
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../differenceInCalendarDays/index'
import convertToFP from '../_lib/convertToFP/index'

var differenceInCalendarDays = convertToFP(fn, 2)

export default differenceInCalendarDays
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../differenceInCalendarISOWeekYears/index'
import convertToFP from '../_lib/convertToFP/index'

var differenceInCalendarISOWeekYears = convertToFP(fn, 2)

export default differenceInCalendarISOWeekYears
export default convertToFP(fn, 2)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@
import fn from '../../differenceInCalendarISOWeeks/index'
import convertToFP from '../_lib/convertToFP/index'

var differenceInCalendarISOWeeks = convertToFP(fn, 2)

export default differenceInCalendarISOWeeks
export default convertToFP(fn, 2)
Loading

0 comments on commit c7ef5f0

Please sign in to comment.