Skip to content

Commit

Permalink
GitHub Issue #103 - convert CRLF to LF line endings (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
oakmac authored Oct 19, 2024
1 parent d5d1302 commit d3fdb4a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/standard-clojure-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@
return s.replace(find, replace)
}

function crlfToLf (txt) {
return txt.replace(/\r\n/g, '\n')
}

function strSplit (str, ch) {
return str.split(ch)
}
Expand Down Expand Up @@ -3780,6 +3784,9 @@
// Parses inputTxt (Clojure code) and returns a String of it formatted according
// to Standard Clojure Style.
function format (inputTxt) {
// replace any CRLF with LF before we do anything
inputTxt = crlfToLf(inputTxt)

// FIXME: wrap this in try/catch and return error code if found
const tree = parse(inputTxt)
const nodesArr = flattenTree(tree)
Expand Down
19 changes: 19 additions & 0 deletions test/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,28 @@ allTestCases.forEach(testCase => {
}
})

const inputFileWithCRLF = fs.readFileSync(path.join(rootDir, 'test_format/line_endings/crlf_input.clj'), 'utf8')
const outputFileWithLF = fs.readFileSync(path.join(rootDir, 'test_format/line_endings/lf_output.clj'), 'utf8')

test('crlf to lf', () => {
expect(isString(inputFileWithCRLF)).toBe(true)
expect(isString(outputFileWithLF)).toBe(true)
expect(inputFileWithCRLF.includes('\r\n')).toBe(true)
expect(outputFileWithLF.includes('\r\n')).toBe(false)

const result = scsLib.format(inputFileWithCRLF)

expect(result.status).toBe('success')
expect(result.out + '\n').toBe(outputFileWithLF)
})

// -----------------------------------------------------------------------------
// Util

function isEnoFile (f) {
return path.extname(f) === '.eno'
}

function isString (s) {
return typeof s === 'string'
}
24 changes: 24 additions & 0 deletions test_format/line_endings/crlf_input.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns wolframite.base.cep
(:require
[wolframite.lib.options :as options]
[wolframite.base.convert :as convert]
[wolframite.base.evaluate :as evaluate]
[wolframite.base.parse :as parse]))

(defn- identity-first [x & _] x)

(defn cep
"Convert-Evaluate-Parse pipeline.
Convert: from clj data to jlink Expr
Evaluate: the Expr on (some) Wolfram Engine
Parse: returned result into clj data.
Each stage can be skipped with appropriate `opts` `:flag` e.g. `:no-parse`"
[expr {:keys [flags]
:as opts}]
(let [convert (if (options/flag?' flags :convert) convert/convert identity-first)
evaluate (if (options/flag?' flags :evaluate) evaluate/evaluate identity-first)
parse (if (options/flag?' flags :parse) parse/parse identity-first)]
(-> expr
(convert opts)
(evaluate opts)
(parse opts))))
24 changes: 24 additions & 0 deletions test_format/line_endings/lf_output.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(ns wolframite.base.cep
(:require
[wolframite.base.convert :as convert]
[wolframite.base.evaluate :as evaluate]
[wolframite.base.parse :as parse]
[wolframite.lib.options :as options]))

(defn- identity-first [x & _] x)

(defn cep
"Convert-Evaluate-Parse pipeline.
Convert: from clj data to jlink Expr
Evaluate: the Expr on (some) Wolfram Engine
Parse: returned result into clj data.
Each stage can be skipped with appropriate `opts` `:flag` e.g. `:no-parse`"
[expr {:keys [flags]
:as opts}]
(let [convert (if (options/flag?' flags :convert) convert/convert identity-first)
evaluate (if (options/flag?' flags :evaluate) evaluate/evaluate identity-first)
parse (if (options/flag?' flags :parse) parse/parse identity-first)]
(-> expr
(convert opts)
(evaluate opts)
(parse opts))))

0 comments on commit d3fdb4a

Please sign in to comment.