Skip to content

Commit

Permalink
feat(editEngine.js): - added optional argument addedFirst to support …
Browse files Browse the repository at this point in the history
…new added records on top, - cor
  • Loading branch information
prakash1998 committed Jun 9, 2020
1 parent 718bfd9 commit 161703b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/editEngine.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import R from 'ramda'

export const generateInitialEditInfo = () => ({
export const generateInitialEditInfo = ({ addedFirst } = {}) => ({
updated: [],
added: [],
addedFirst,
removed: [],
// originalRow => editedRow
updatedMap: new Map(),
Expand Down Expand Up @@ -205,8 +206,8 @@ export const undo = (editInfo = generateInitialEditInfo()) =>
export const isDirty = (editInfo = generateInitialEditInfo()) =>
editInfo.added.length > 0 || editInfo.removed.length > 0 || editInfo.updated.length > 0

export const validateData = ({ orignalData, editInfo = generateInitialEditInfo(), headers }) => {
const editedData = applyEdits({ data: orignalData, editInfo })
export const validateData = ({ originalData, editInfo = generateInitialEditInfo(), headers }) => {
const editedData = applyEdits({ data: originalData, editInfo })
return validateEditedData({ editedData, headers })
}

Expand Down Expand Up @@ -235,4 +236,8 @@ export const apply = editInfo =>
)

export const applyEdits = ({ data, editInfo }) =>
isDirty(editInfo) ? [...apply(editInfo)(data), ...editInfo.added] : data
isDirty(editInfo)
? editInfo.addedFirst
? [...editInfo.added, ...apply(editInfo)(data)]
: [...apply(editInfo)(data), ...editInfo.added]
: data
6 changes: 3 additions & 3 deletions src/editEngine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ test('should validate data properly', () => {
editedRow: addedRow1,
})

const errors1 = editEngine.validateData({ orignalData: [], editInfo: addedInfo1, headers })
const errors1 = editEngine.validateData({ originalData: [], editInfo: addedInfo1, headers })
expect(errors1.length).toBe(1)

const addedRow2 = {
Expand All @@ -280,7 +280,7 @@ test('should validate data properly', () => {
editedRow: addedRow2,
})

const errors2 = editEngine.validateData({ orignalData: [], editInfo: addedInfo2, headers })
const errors2 = editEngine.validateData({ originalData: [], editInfo: addedInfo2, headers })
expect(errors2.length).toBe(0)

const headers2 = [
Expand All @@ -295,7 +295,7 @@ test('should validate data properly', () => {
]

const errors3 = editEngine.validateData({
orignalData: [],
originalData: [],
editInfo: addedInfo2,
headers: headers2,
})
Expand Down

0 comments on commit 161703b

Please sign in to comment.