Skip to content

Commit

Permalink
feat: add extraInfo parameter to codeGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Mar 18, 2019
1 parent 049b970 commit f698baf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function index(
type CodeGenerator = (
parsedPath: ParsedPath,
changeCase: ChangeCase,
extraInfo: ExtraInfo,
) => string

interface ParsedPath {
Expand All @@ -95,6 +96,17 @@ function index(
interface ChangeCase {
// See https://github.com/blakeembrey/change-case#usage
}

interface ExtraInfo {
/** total number of items */
total: number,
/** index of current item */
index: number,
/** if current item is the first */
first: boolean,
/** if current item is the last */
last: boolean,
}
```

See [all changeCase methods](https://github.com/blakeembrey/change-case#usage).
Expand Down
19 changes: 17 additions & 2 deletions src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ type Pattern = string
type CodeGenerator = (
parsedPath: ParsedPath,
changeCase: ChangeCase,
extraInfo: {
total: number,
index: number,
first: boolean,
last: boolean,
},
) => string

interface Marker {
Expand Down Expand Up @@ -69,14 +75,23 @@ export default class Generator {
)
const codes = paths
.filter(path => path !== currentFile)
.map(path => {
.map((path, index, paths) => {
const pp = p.parse(path)
const parsedPath: ParsedPath = {
path: getRelativePath(currentDir, p.join(pp.dir, pp.name)),
name: pp.name,
ext: pp.ext,
}
const code = marker.codeGenerator(parsedPath, changeCase)
const code = marker.codeGenerator(
parsedPath,
changeCase,
{
total: paths.length,
index: index,
first: index === 0,
last: index === paths.length - 1,
},
)
return marker.indent + code
})
edit.replace(
Expand Down

0 comments on commit f698baf

Please sign in to comment.