Skip to content

Commit

Permalink
feat: add time() (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
anikghosh256 committed Apr 2, 2024
1 parent 1e48a33 commit 61e85a1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ console.log(compiled); // you can also write to a file or do whatever you want w
- `singularize(string)` - Change the word to its singular
- `capPluralize(string)` - capitalize + pluralize
- `capSingularize(string)` - capitalize + singularize
- `time()` - return timestamp


## Notes
Expand Down
24 changes: 23 additions & 1 deletion helpers/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,26 @@ exports.singularize = function (str) {
exports.capSingularize = function (str) {
str = exports.capitalize(str)
return pluralize.singular(str)
}
}

// date helpers

// time
exports.time = function (time = 'getTime') {
const dateMethods = {
getTime: 'getTime',
getDate: 'getDate',
getDay: 'getDay',
getFullYear: 'getFullYear',
getHours: 'getHours',
now: 'getTime'
}

if (dateMethods[time]) {
return new Date()[dateMethods[time]]()
}
}

exports.nonArgumentFunctions = {
time: exports.time
}
60 changes: 30 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
const fs = require('fs')
const functions = require('./helpers/functions')


/**
* Read file and compile it with variables.
*
*
* @param {string} filePath - Path to file.
* @param {object} variables - Variables to compile.
* @returns {string} Compiled content.
*/
function compile(filePath, variables = {}) {
// Read the content of the file
const content = fs.readFileSync(filePath, 'utf8')

// Regex pattern for variables. It can be ${name} or ${f(name)}
const pattern = /\$\{(\w+|\w+\(\w+\))\}/g
// Replace all variables in the content with their corresponding values
const replacedContent = content.replace(pattern, (match, variable) => {

// check if has a function in pattern
const functionMatch = variable.match(/\w+\((\w+)\)/)
if (functionMatch) {
const functionName = variable.split('(')[0]
const functionArg = functionMatch[1]
const functionArgValue = variables[functionArg]

if (functions.hasOwnProperty(functionName) && functionArgValue) {
return functions[functionName](functionArgValue)
}
// Read the content of the file
const content = fs.readFileSync(filePath, 'utf8')

// Regex pattern for variables. It can be ${name} or ${f(name)}
const pattern = /\$\{(\w+|\w+\(\w+\))\}/g

// Replace all variables in the content with their corresponding values
const replacedContent = content.replace(pattern, (match, variable) => {
// check if has a function in pattern
const functionMatch = variable.match(/\w+\((\w+)\)/)
if (functionMatch) {
const functionName = variable.split('(')[0]
const functionArg = functionMatch[1]
const functionArgValue = variables[functionArg]

if (functions.hasOwnProperty(functionName) && functionArgValue) {
return functions[functionName](functionArgValue)
} else if (functions.nonArgumentFunctions.hasOwnProperty(functionName)) {
return functions[functionName]()
}
}

if (variables.hasOwnProperty(variable)) {
return variables[variable]
}
if (variables.hasOwnProperty(variable)) {
return variables[variable]
}

return match
})

return match
})

return replacedContent
}
return replacedContent
}

module.exports = compile
module.exports = compile
15 changes: 14 additions & 1 deletion test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ describe('compile', () => {
const filePath = join(__dirname, 'template.ct')
const expectedFilePath = join(__dirname, 'expected.ct')
it('should compile', () => {
const result = compile(filePath, { name: 'compiled', book: "book", books: "books" }).trim()
const result = compile(filePath, {
name: 'compiled',
book: 'book',
books: 'books'
}).trim()
const receivedString = fs.readFileSync(expectedFilePath, 'utf8').trim()

expect(result).toMatch(receivedString)
})
})

describe('compile', () => {
const filePath = join(__dirname, 'time-template.ct')

it('should return time', () => {
const result = compile(filePath, {}).trim()

// check if the result is a number string
expect(parseInt(result)).not.toBeNaN()
})
})
1 change: 1 addition & 0 deletions test/time-template.ct
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
${time(now)}

0 comments on commit 61e85a1

Please sign in to comment.