Skip to content

Commit

Permalink
Merge pull request #51 from pacdiv/feat/simplify-core-use
Browse files Browse the repository at this point in the history
Simplified the use of the core
  • Loading branch information
pacdiv committed Oct 4, 2019
2 parents a8ee354 + c060377 commit 8c10128
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 99 deletions.
6 changes: 2 additions & 4 deletions src/components/playground/Playground/Playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ class Playground extends Component {
.addCondition(specs, realIndex, Boolean(position))
.then(() =>
this.setState({
chunks: this.core.getChunks(),
isEditingAtIndex: null,
regexChunks: this.core.getRegexChunks(),
...this.core.getChunksState(),
})
)
.catch(err => Promise.reject(err))
Expand All @@ -79,10 +78,9 @@ class Playground extends Component {
.deleteCondition(index)
.then(() =>
this.setState({
chunks: this.core.getChunks(),
isEditingAtIndex: null,
regexChunks: this.core.getRegexChunks(),
sentenceEl: null,
...this.core.getChunksState(),
})
)
.catch(err => Promise.reject(err))
Expand Down
116 changes: 45 additions & 71 deletions src/lib/core/core.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { anchors, characters } from "./data"
import conditionAdders from "./condition-adders"
import {
getAnchorsWithoutUsedEdges,
getAvailableAnchors,
getAvailableBackReferences,
getAvailableDefaultCharacters,
insertNewChunk,
updateExistingChunk,
} from "./utils"
Expand Down Expand Up @@ -42,20 +44,18 @@ async function addCondition(specs, insertAtIndex = 0, newItem = true) {
const insertChunk = newItem ? insertNewChunk : updateExistingChunk
this.chunks = insertChunk(this.chunks, insertAtIndex, chunk)

this.regexChunks = this.chunks.map(({ regex }) => regex)
this.capturedChunks = this.chunks.reduce((acc, { regex, specs }, index) => {
const key = `\\${acc.length + 1}`
return specs.capturedExpression === "YES"
? acc.concat({ index, label: regex, key, regex: key })
: acc
}, [])

return Promise.resolve()
} catch (err) {
return Promise.reject(err)
}
}

function deleteAllConditions() {
this.chunks = []
this.regexChunks = []
this.capturedChunks = []
}

function deleteCondition(targetIndex) {
if (!this.chunks[targetIndex]) {
return Promise.reject(
Expand All @@ -64,107 +64,81 @@ function deleteCondition(targetIndex) {
}

this.chunks = this.chunks.filter((chunk, index) => targetIndex !== index)
this.regexChunks = this.chunks.map(({ regex }) => regex)
return Promise.resolve()
}

function getAvailableAnchors(targetIndex, position) {
const chunksLength = this.chunks.length

if (!chunksLength) return Array.from(anchors)

const data = getAnchorsWithoutUsedEdges(this.chunks, anchors, [
"STARTS_WITH",
"ENDS_WITH",
])

if (position === "before") {
return targetIndex === 0
? data.filter(item => item.key !== "ENDS_WITH")
: data.filter(item => item.key === "CONTAINS")
} else if (position === "after") {
return targetIndex === chunksLength - 1
? data.filter(item => item.key !== "STARTS_WITH")
: data.filter(item => item.key === "CONTAINS")
}

return data.filter(item => item.key === "CONTAINS")
}

function getAvailableData(targetIndex, position) {
return {
availableAnchors: this.getAvailableAnchors(targetIndex, position),
availableBackReferences: this.getAvailableBackReferences(
availableAnchors: getAvailableAnchors.call(this, targetIndex, position),
availableBackReferences: getAvailableBackReferences.call(
this,
targetIndex,
position
),
availableDefaultCharacters: this.getAvailableDefaultCharacters(
availableDefaultCharacters: getAvailableDefaultCharacters.call(
this,
targetIndex,
position
),
}
}

function getAvailableBackReferences(targetIndex, position) {
if (targetIndex < 0) return []

return this.capturedChunks.filter(
chunk =>
(!position && chunk.index < targetIndex) ||
(position === "before" && chunk.index < targetIndex) ||
(position === "after" && chunk.index <= targetIndex)
)
}

function getAvailableDefaultCharacters(targetIndex, position) {
return !this.getAvailableBackReferences(targetIndex, position).length
? characters.DEFAULT.filter(item => item.key !== "BACK_REFERENCES")
: characters.DEFAULT
}

function getChunks() {
return Array.from(this.chunks)
function getChunksState() {
return {
chunks: Array.from(this.chunks),
regexChunks: Array.from(this.regexChunks),
}
}

function getFlags() {
return JSON.parse(JSON.stringify(this.flags))
}

function getRegexChunks() {
return Array.from(this.regexChunks)
}

function setFlag(entry, value) {
this.flags = {
...this.flags,
[entry]: value,
}
}

function deleteAllConditions() {
this.chunks = []
this.regexChunks = []
this.capturedChunks = []
function execFuncWhileSyncChunksArrays(fn, args) {
return fn
.apply(this, args)
.then(() => {
this.regexChunks = this.chunks.map(({ regex }) => regex)
this.capturedChunks = this.chunks.reduce(
(acc, { regex, specs }, index) => {
const key = `\\${acc.length + 1}`
return specs.capturedExpression === "YES"
? acc.concat({ index, label: regex, key, regex: key })
: acc
},
[]
)

return Promise.resolve()
})
.catch(err => Promise.reject(err))
}

const core = {
addCondition,
addCondition: function() {
return execFuncWhileSyncChunksArrays.call(this, addCondition, arguments)
},
deleteCondition: function() {
return execFuncWhileSyncChunksArrays.call(this, deleteCondition, arguments)
},
capturedChunks: [],
chunks: [],
deleteCondition,
deleteAllConditions,
flags: {
global: false,
},
getAvailableAnchors,
getAvailableBackReferences,
getAvailableData,
getAvailableDefaultCharacters,
getChunks,
getChunksState,
getFlags,
getRegexChunks,
regexChunks: [],
setFlag,
deleteAllConditions,
}

export default core
52 changes: 29 additions & 23 deletions src/lib/core/core.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,27 @@ describe("Core", () => {
const core = Object.create(Core)

expect(core).toBeTruthy()
expect(core.getChunks()).toEqual([])
expect(core.getChunksState().chunks).toEqual([])
expect(core.getFlags().global).toEqual(false)
expect(core.getRegexChunks()).toEqual([])
expect(core.getChunksState().regexChunks).toEqual([])
})

it("add condition", async () => {
const core = Object.create(Core)

await core.addCondition(condition1)

expect(core.getChunks().length).toEqual(1)
expect(core.getRegexChunks().length).toEqual(1)
expect(core.getChunksState().chunks.length).toEqual(1)
expect(core.getChunksState().regexChunks.length).toEqual(1)

const wrongCondition = {
...condition2,
minimumQuantifierValue: "-1",
}

core.addCondition(wrongCondition).catch(() => {
expect(core.getChunks().length).toEqual(1)
expect(core.getRegexChunks().length).toEqual(1)
expect(core.getChunksState().chunks.length).toEqual(1)
expect(core.getChunksState().regexChunks.length).toEqual(1)
})
})

Expand All @@ -52,8 +52,8 @@ describe("Core", () => {
await core.addCondition(condition1)
await core.addCondition(condition1, 0, false)

expect(core.getChunks().length).toEqual(1)
expect(core.getRegexChunks().length).toEqual(1)
expect(core.getChunksState().chunks.length).toEqual(1)
expect(core.getChunksState().regexChunks.length).toEqual(1)
})

it("delete condition", async () => {
Expand All @@ -65,12 +65,12 @@ describe("Core", () => {
await core.deleteCondition(1)
await core.deleteCondition(0)

expect(core.getChunks().length).toEqual(0)
expect(core.getRegexChunks().length).toEqual(0)
expect(core.getChunksState().chunks.length).toEqual(0)
expect(core.getChunksState().regexChunks.length).toEqual(0)

core.deleteCondition(42).catch(() => {
expect(core.getChunks().length).toEqual(0)
expect(core.getRegexChunks().length).toEqual(0)
expect(core.getChunksState().chunks.length).toEqual(0)
expect(core.getChunksState().regexChunks.length).toEqual(0)
})
})

Expand All @@ -85,22 +85,28 @@ describe("Core", () => {
await core.addCondition(condition1)
await core.addCondition(condition2)

expect(joinKeys(core.getAvailableAnchors(0, "before"))).toEqual(
"CONTAINS-STARTS_WITH"
expect(
joinKeys(core.getAvailableData(0, "before").availableAnchors)
).toEqual("CONTAINS-STARTS_WITH")
expect(
joinKeys(core.getAvailableData(1, "before").availableAnchors)
).toEqual("CONTAINS")

expect(
joinKeys(core.getAvailableData(0, "after").availableAnchors)
).toEqual("CONTAINS")
expect(
joinKeys(core.getAvailableData(1, "after").availableAnchors)
).toEqual("CONTAINS-ENDS_WITH")

expect(joinKeys(core.getAvailableData(0).availableAnchors)).toEqual(
"CONTAINS"
)
expect(joinKeys(core.getAvailableAnchors(1, "before"))).toEqual("CONTAINS")

expect(joinKeys(core.getAvailableAnchors(0, "after"))).toEqual("CONTAINS")
expect(joinKeys(core.getAvailableAnchors(1, "after"))).toEqual(
"CONTAINS-ENDS_WITH"
)

expect(joinKeys(core.getAvailableAnchors(0))).toEqual("CONTAINS")

await core.deleteCondition(1)
await core.deleteCondition(0)

expect(joinKeys(core.getAvailableAnchors(0))).toEqual(
expect(joinKeys(core.getAvailableData(0).availableAnchors)).toEqual(
"CONTAINS-ENDS_WITH-STARTS_WITH"
)
})
Expand Down
44 changes: 43 additions & 1 deletion src/lib/core/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const getAnchorsWithoutUsedEdges = (dataSource, anchors, edges) => {
import { anchors, characters } from "./data"

const getAnchorsWithoutUsedEdges = (dataSource, anchors, edges) => {
return edges.reduce(
(acc, anchor) =>
dataSource.some(({ specs }) => specs.anchor === anchor)
Expand All @@ -8,6 +10,46 @@ export const getAnchorsWithoutUsedEdges = (dataSource, anchors, edges) => {
)
}

export function getAvailableAnchors(targetIndex, position) {
const chunksLength = this.chunks.length

if (!chunksLength) return Array.from(anchors)

const data = getAnchorsWithoutUsedEdges(this.chunks, anchors, [
"STARTS_WITH",
"ENDS_WITH",
])

if (position === "before") {
return targetIndex === 0
? data.filter(item => item.key !== "ENDS_WITH")
: data.filter(item => item.key === "CONTAINS")
} else if (position === "after") {
return targetIndex === chunksLength - 1
? data.filter(item => item.key !== "STARTS_WITH")
: data.filter(item => item.key === "CONTAINS")
}

return data.filter(item => item.key === "CONTAINS")
}

export function getAvailableBackReferences(targetIndex, position) {
if (targetIndex < 0) return []

return this.capturedChunks.filter(
chunk =>
(!position && chunk.index < targetIndex) ||
(position === "before" && chunk.index < targetIndex) ||
(position === "after" && chunk.index <= targetIndex)
)
}

export function getAvailableDefaultCharacters(targetIndex, position) {
return !getAvailableBackReferences.call(this, targetIndex, position).length
? characters.DEFAULT.filter(item => item.key !== "BACK_REFERENCES")
: characters.DEFAULT
}

export const getLabelFromKey = (dataSource, targetKey, withCap = false) => {
const { label } = dataSource.find(({ key }) => targetKey === key) || {}

Expand Down

0 comments on commit 8c10128

Please sign in to comment.