negative modulo (v93)

Revision 93 of this benchmark created on


Preparation HTML

<script scr="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>

<script>



var regex = /\${([^}]*)}/g;

function parseTemplateObject(templateObject, context){
  var startTime = Date.now();

  var {
    window,
    document,
    setTimeout,
    setInterval,
    currentRecord,
    currentUser,
    iteratorObject,
    router,
    parentRecord,
    resources,
  } = context;
 
 console.log(templateObject,'T');
 try{
  if (_.isEmpty(templateObject)) {
    return templateObject;
  }

  function recursivelyIterate(object, acc = {}) {
    if (!_.isObject(object)) {
      return object.replace(regex, (match, group) => eval(`${group}`));
    }

    _.forEach(object, (value, key) => {
      if (_.isObject(value)) {
        if (Array.isArray(value)) {
          acc[key] = value.map(element => recursivelyIterate(element));
        } else {
          acc[key] = {}; // currentRecord:{}
          //f the property is an object, recursively iterate over its properties
          acc[key] = recursivelyIterate(value);
        }
      } else {
        // eslint-disable-next-line no-lonely-if -- legacy code
        if (typeof value === 'string') {
          //only exp
          if (/^\$\{[^}]*\}$/.test(value)) {
            acc[key] = eval(value.substring(2, value.length - 1));
          } else {
            //exp having some string appended or prepended
            acc[key] = value.replace(regex, (match, group) => eval(`${group}`));
          }
        } else {
          acc[key] = value;
        }
      }
    });

    return acc;
  };

  return recursivelyIterate(templateObject);
  }
  catch(e){
  	console.error(e);
  }
};

function templateObject2(
  templateObject,
  context
){
	console.log('test',templateObject,context,_);
  if (_.isEmpty(templateObject)) {
    return templateObject;
  }

  try {
    return JSON.parse(_.template(JSON.stringify(templateObject))(context)) ??{};
  } catch (e) {
    return templateObject;
  }
};


</script>

Test runner

Ready to run.

Testing in
TestOps/sec
eval approach
const TEMPLATE_OBJECT = {
  id: "household_record_page",
  name: null,
  isGlobal: null,
  entityType: "_s_Household",
  grants: [],
  order: null,
  roleIds: null,
  buttons: null,
  createdTime: 1705496417454,
  modifiedTime: 1705496417453,
  children: ["root"],
  allComponents: {
    "0": {
      langVsTranslatedFieldValues: null,
      id: "0",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: null,
      order: 0,
      children: ["1", "2", "3"],
      props: {
        pageTemplate: "STICKY_HEADER_TWO_COLS_PINNED_LEFT",
        colGap: 20,
        leftColSpan: 13,
        rightColSpan: 7,
        className: "spr-ui-05 pr-4",
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    "1": {
      langVsTranslatedFieldValues: null,
      id: "1",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: "ONE",
      order: 0,
      children: ["1a", "1b"],
      props: {
        pageTemplate: "ONE_REGION",
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    root: {
      langVsTranslatedFieldValues: null,
      id: "root",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: null,
      order: 0,
      children: ["0"],
      props: {
        pageTemplate: "ONE_REGION",
        stickyHeaderPresent: true,
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    "1a": {
      langVsTranslatedFieldValues: null,
      id: "1a",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: null,
      order: 0,
      children: ["1a1"],
      props: {
        pageTemplate: "ONE_REGION",
        className: "ml-4 pr-4",
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    "1a1": {
      langVsTranslatedFieldValues: null,
      id: "1a1",
      templateId: "@sprinklr/widget/EntityControls",
      persistedId: null,
      name: "ONE",
      order: 0,
      children: null,
      props: {
        entityType: "_s_Household",
        upfrontActionsCount: 4,
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: [
        {
          langVsTranslatedFieldValues: null,
          id: "OPEN_MEETING_RECORD_FORM",
          templateId: "@sprinklr/action/OpenRecordForm",
          persistedId: null,
          label: "Schedule Meeting",
          detail: null,
          icon: "meeting",
          entityType: "_s_Meeting",
          action: null,
          props: {
            type: "ICON_TEXT_BUTTON",
            recordFormVariant: "THIRD_PANE",
            recordFormLayoutId: "finance_meeting",
            title: "Schedule Meeting",
            context: {
              linkedEntityId: "_s_Household-:-${currentRecord?.entityId}",
              attendees: "${currentRecord?.attendees}",
            },
          },
          translations: null,
          children: null,
          visibility: null,
          disabilityFilter: null,
          payload: null,
        },
        {
          langVsTranslatedFieldValues: null,
          id: "OPEN_INTERACTION_RECORD_FORM",
          templateId: "@sprinklr/action/OpenRecordForm",
          persistedId: null,
          label: "Log Interaction",
          detail: null,
          icon: "interaction",
          entityType: "_s_Interaction",
          action: null,
          props: {
            type: "ICON_TEXT_BUTTON",
            recordFormVariant: "THIRD_PANE",
            recordFormLayoutId: "finance_interaction",
            title: "Log Interaction",
            messageOnSuccess: "Successfully created the interaction",
            context: {
              linkedEntityId: "_s_Household-:-${currentRecord?.entityId}",
              attendees: [
                {
                  linkedEntityId:
                    "_s_Customer-:-${currentRecord?.values?._c_headCustomerId}",
                },
              ],
              relatedTo: [
                "_s_Customer-:-${currentRecord?.values?._c_headCustomerId}",
              ],
            },
          },
          translations: null,
          children: null,
          visibility: null,
          disabilityFilter: null,
          payload: null,
        },
        {
          langVsTranslatedFieldValues: null,
          id: "OPEN_SUPERVISOR_REVIEW_RECORD_FORM",
          templateId: "@sprinklr/action/OpenRecordForm",
          persistedId: null,
          label: "Add Review",
          detail: null,
          icon: "interaction",
          entityType: "_c_reviewforsupervisor",
          action: null,
          props: {
            type: "ICON_TEXT_BUTTON",
            recordFormVariant: "THIRD_PANE",
            recordFormLayoutId: "household_review_form",
            title: "Add Review",
            messageOnSuccess: "Successfully created the review",
            context: {
              _c_wealthgroupidforreview: "${currentRecord?.entityId}",
              name: "${currentRecord?.name} Review",
              _c_wealthgroupheadforreview:
                "${currentRecord?.values?._c_headCustomerId}",
              _c_rm_user:
                "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/WRM') && e?.startsWith('USER/'))?.split('/')?.[1]}",
            },
          },
          translations: null,
          children: null,
          visibility: {
            filterType: "AND",
            filters: [],
            filters_dummy: [
              {
                filterType: "EXPRESSION",
                field:
                  "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/WRM') && e?.startsWith('USER/'))?.split('/')?.[1] !== currentUser.userId}",
              },
              {
                filterType: "EXPRESSION",
                field:
                  "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/IA') && e?.startsWith('USER/'))?.split('/')?.[1] !== currentUser.userId}",
              },
              {
                filterType: "EXPRESSION",
                field:
                  "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/SRM') && e?.startsWith('USER/'))?.split('/')?.[1] !== currentUser.userId}",
              },
            ],
          },
          disabilityFilter: null,
          payload: null,
        },
        {
          langVsTranslatedFieldValues: null,
          id: "INITIATE_WORKFLOW",
          templateId: "@sprinklr/action/GuidedAction",
          persistedId: null,
          label: "Initiate Workflow",
          detail: null,
          icon: "workflow",
          entityType: "_s_Household",
          action: "254937",
          props: {
            type: "ICON_TEXT_BUTTON",
          },
          translations: null,
          children: null,
          visibility: null,
          disabilityFilter: null,
          payload: null,
        },
      ],
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
  },
};

const CONTEXT = {
  currentRecord: {
    attendees: [{ id: "1", name: "Nik" }],
    values: {
      _c_headCustomerId: "123",
    },
    entityId: "123",
    name: "Nikunj",
    shares: { jointGrants: ["USER/123/WRM"] },
  },
  currentUser: {
    userId: 123,
  },
};



parseTemplateObject(TEMPLATE_OBJECT, CONTEXT);
ready
_template
const TEMPLATE_OBJECT = {
  id: "household_record_page",
  name: null,
  isGlobal: null,
  entityType: "_s_Household",
  grants: [],
  order: null,
  roleIds: null,
  buttons: null,
  createdTime: 1705496417454,
  modifiedTime: 1705496417453,
  children: ["root"],
  allComponents: {
    "0": {
      langVsTranslatedFieldValues: null,
      id: "0",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: null,
      order: 0,
      children: ["1", "2", "3"],
      props: {
        pageTemplate: "STICKY_HEADER_TWO_COLS_PINNED_LEFT",
        colGap: 20,
        leftColSpan: 13,
        rightColSpan: 7,
        className: "spr-ui-05 pr-4",
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    "1": {
      langVsTranslatedFieldValues: null,
      id: "1",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: "ONE",
      order: 0,
      children: ["1a", "1b"],
      props: {
        pageTemplate: "ONE_REGION",
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    root: {
      langVsTranslatedFieldValues: null,
      id: "root",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: null,
      order: 0,
      children: ["0"],
      props: {
        pageTemplate: "ONE_REGION",
        stickyHeaderPresent: true,
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    "1a": {
      langVsTranslatedFieldValues: null,
      id: "1a",
      templateId: "@sprinklr/widget/Layout",
      persistedId: null,
      name: null,
      order: 0,
      children: ["1a1"],
      props: {
        pageTemplate: "ONE_REGION",
        className: "ml-4 pr-4",
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: null,
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
    "1a1": {
      langVsTranslatedFieldValues: null,
      id: "1a1",
      templateId: "@sprinklr/widget/EntityControls",
      persistedId: null,
      name: "ONE",
      order: 0,
      children: null,
      props: {
        entityType: "_s_Household",
        upfrontActionsCount: 4,
      },
      context: null,
      uiEvents: null,
      formLayout: null,
      tableLayout: null,
      buttons: [
        {
          langVsTranslatedFieldValues: null,
          id: "OPEN_MEETING_RECORD_FORM",
          templateId: "@sprinklr/action/OpenRecordForm",
          persistedId: null,
          label: "Schedule Meeting",
          detail: null,
          icon: "meeting",
          entityType: "_s_Meeting",
          action: null,
          props: {
            type: "ICON_TEXT_BUTTON",
            recordFormVariant: "THIRD_PANE",
            recordFormLayoutId: "finance_meeting",
            title: "Schedule Meeting",
            context: {
              linkedEntityId: "_s_Household-:-${currentRecord?.entityId}",
              attendees: "${currentRecord?.attendees}",
            },
          },
          translations: null,
          children: null,
          visibility: null,
          disabilityFilter: null,
          payload: null,
        },
        {
          langVsTranslatedFieldValues: null,
          id: "OPEN_INTERACTION_RECORD_FORM",
          templateId: "@sprinklr/action/OpenRecordForm",
          persistedId: null,
          label: "Log Interaction",
          detail: null,
          icon: "interaction",
          entityType: "_s_Interaction",
          action: null,
          props: {
            type: "ICON_TEXT_BUTTON",
            recordFormVariant: "THIRD_PANE",
            recordFormLayoutId: "finance_interaction",
            title: "Log Interaction",
            messageOnSuccess: "Successfully created the interaction",
            context: {
              linkedEntityId: "_s_Household-:-${currentRecord?.entityId}",
              attendees: [
                {
                  linkedEntityId:
                    "_s_Customer-:-${currentRecord?.values?._c_headCustomerId}",
                },
              ],
              relatedTo: [
                "_s_Customer-:-${currentRecord?.values?._c_headCustomerId}",
              ],
            },
          },
          translations: null,
          children: null,
          visibility: null,
          disabilityFilter: null,
          payload: null,
        },
        {
          langVsTranslatedFieldValues: null,
          id: "OPEN_SUPERVISOR_REVIEW_RECORD_FORM",
          templateId: "@sprinklr/action/OpenRecordForm",
          persistedId: null,
          label: "Add Review",
          detail: null,
          icon: "interaction",
          entityType: "_c_reviewforsupervisor",
          action: null,
          props: {
            type: "ICON_TEXT_BUTTON",
            recordFormVariant: "THIRD_PANE",
            recordFormLayoutId: "household_review_form",
            title: "Add Review",
            messageOnSuccess: "Successfully created the review",
            context: {
              _c_wealthgroupidforreview: "${currentRecord?.entityId}",
              name: "${currentRecord?.name} Review",
              _c_wealthgroupheadforreview:
                "${currentRecord?.values?._c_headCustomerId}",
              _c_rm_user:
                "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/WRM') && e?.startsWith('USER/'))?.split('/')?.[1]}",
            },
          },
          translations: null,
          children: null,
          visibility: {
            filterType: "AND",
            filters: [],
            filters_dummy: [
              {
                filterType: "EXPRESSION",
                field:
                  "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/WRM') && e?.startsWith('USER/'))?.split('/')?.[1] !== currentUser.userId}",
              },
              {
                filterType: "EXPRESSION",
                field:
                  "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/IA') && e?.startsWith('USER/'))?.split('/')?.[1] !== currentUser.userId}",
              },
              {
                filterType: "EXPRESSION",
                field:
                  "${currentRecord?.shares?.jointGrants?.find(e => e?.endsWith('/SRM') && e?.startsWith('USER/'))?.split('/')?.[1] !== currentUser.userId}",
              },
            ],
          },
          disabilityFilter: null,
          payload: null,
        },
        {
          langVsTranslatedFieldValues: null,
          id: "INITIATE_WORKFLOW",
          templateId: "@sprinklr/action/GuidedAction",
          persistedId: null,
          label: "Initiate Workflow",
          detail: null,
          icon: "workflow",
          entityType: "_s_Household",
          action: "254937",
          props: {
            type: "ICON_TEXT_BUTTON",
          },
          translations: null,
          children: null,
          visibility: null,
          disabilityFilter: null,
          payload: null,
        },
      ],
      valueSource: null,
      evaluatedValue: null,
      translations: null,
    },
  },
};

const CONTEXT = {
  currentRecord: {
    attendees: [{ id: "1", name: "Nik" }],
    values: {
      _c_headCustomerId: "123",
    },
    entityId: "123",
    name: "Nikunj",
    shares: { jointGrants: ["USER/123/WRM"] },
  },
  currentUser: {
    userId: 123,
  },
};



templateObject2(TEMPLATE_OBJECT, CONTEXT);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.