Skip to content

Commit

Permalink
Merge branch 'fix-unique-id-gen'
Browse files Browse the repository at this point in the history
* fix-unique-id-gen:
  Fix unique field id generation algorithm. Unique id of every element is derived from 1. parentid 2. position index of current element.
  • Loading branch information
harish2704 committed Jul 21, 2020
2 parents e9f1e65 + e6fb04d commit d339a65
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 36 deletions.
45 changes: 10 additions & 35 deletions src/vfjs-global-mixin/methods/vfjs-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,6 @@ const vfjsHelpers = {
},
}));
},
vfjsHelperHashString(string, binary = 62) {
let integer = 0;

for (let i = 0; i < string.length; i++) {
const char = string.charCodeAt(i);
integer = (integer * 33) ^ char; // eslint-disable-line no-bitwise
}

// Convert int to unsigned to get a positive number
integer >>>= 0; // eslint-disable-line no-bitwise

const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const array = [];

// Create an alphanumeric hash of unsigned int
while (integer >= binary) {
const char = integer % binary;
array.push(chars[char]);
integer = Math.floor(integer / binary);
}

return array.join('');
},
vfjsHelperCreateComponent({ children = [], component, props }) {
// If the component matches one of the local components
// passed in with the `components` prop
Expand Down Expand Up @@ -138,25 +115,23 @@ const vfjsHelpers = {
set(newVfjsModel, key, value);
return newVfjsModel;
},
// The level param helps us to differentiate further between fields.
// As the same field configuration may be present throughout the ui schema
// and thus have the same hash.
//
// We mediate this by providing the depth level as a second param
// which will make the hash unique for every field
vfjsHelperGenerateField(field, level = 0) {
/*
* Unique id for every element is generated by simply appending the position index with parentid.
* The parentId is the unique id of parent component ( it can be either a form or a field )
* The id of top most form component is auto generated using Math.random.
* Since id is a function of parentId and element index, we will get consistent , but unique id for each component
*/
vfjsHelperGenerateField(field, parentId) {
if (!field) {
return false;
}

const { children = [], ...fieldWithoutChildren } = field;
const objString = JSON.stringify({ fieldWithoutChildren, level });
const id = this.vfjsHelperHashString(objString);
const { children = [] } = field;

return {
...field,
id,
children: children.map((child, i) => this.vfjsHelperGenerateField(child, (i + 1) * (level + 1))),
id: parentId,
children: children.map((child, i) => this.vfjsHelperGenerateField(child, `${parentId}-${i}`)),
};
},
vfjsHelperChildArrayMapper({ model, children = [], ...child }, parentModel, index) {
Expand Down
2 changes: 1 addition & 1 deletion src/vfjs-global-mixin/methods/vfjs-ui/setters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { VFJS_EVENT_STATE_UPDATED } from '../../../constants';
const vfjsUiSetters = {
setVfjsUiSchema(uiSchema = []) {
const newVfjsUiSchema = uiSchema.reduce(
(fields, field, index) => [...fields, this.vfjsHelperGenerateField(field, index)],
(fields, field, index) => [...fields, this.vfjsHelperGenerateField(field, `${this.id}-${index}`)],
[],
);

Expand Down
4 changes: 4 additions & 0 deletions src/vfjs-global-mixin/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const props = {
type: Object,
default: () => ({}),
},
id: {
type: String,
default: () => Math.random().toString(36).substr(2, 9),
},
options: {
type: Object,
default: () => ({}),
Expand Down

0 comments on commit d339a65

Please sign in to comment.