Skip to content

Instantly share code, notes, and snippets.

@saveroo
Last active October 4, 2019 02:08
Show Gist options
  • Save saveroo/bd85f3a2f382b0dd9dc43bd148a1708d to your computer and use it in GitHub Desktop.
Save saveroo/bd85f3a2f382b0dd9dc43bd148a1708d to your computer and use it in GitHub Desktop.
Juz>Memorized>Chapter>Verses Mapper // Regarding handling Mapped process, from memorization[{chapter[]}] to juz[memorization[chapter{verses[]}]
let neededData = {
ajza: [
1,
149,
260,
386,
517,
641,
751,
900,
1042,
1201,
1328,
1479,
1649,
1803,
2030,
2215,
2484,
2674,
2876,
3215,
3386,
3564,
3733,
4090,
4265,
4511,
4706,
5105,
5242,
5673,
6236,
],
juzMapping: [
{
id: 1,
juz_number: 1,
verse_mapping: {
'1': '1-7',
'2': '1-141',
},
},
{
id: 2,
juz_number: 2,
verse_mapping: {
'2': '142-252',
},
},
{
id: 3,
juz_number: 3,
verse_mapping: {
'2': '253-286',
'3': '1-92',
},
},
{
id: 4,
juz_number: 4,
verse_mapping: {
'3': '93-200',
'4': '1-23',
},
},
{
id: 5,
juz_number: 5,
verse_mapping: {
'4': '24-147',
},
},
{
id: 6,
juz_number: 6,
verse_mapping: {
'4': '148-176',
'5': '1-81',
},
},
{
id: 7,
juz_number: 7,
verse_mapping: {
'5': '82-120',
'6': '1-110',
},
},
{
id: 8,
juz_number: 8,
verse_mapping: {
'6': '111-165',
'7': '1-87',
},
},
{
id: 9,
juz_number: 9,
verse_mapping: {
'7': '88-206',
'8': '1-40',
},
},
{
id: 10,
juz_number: 10,
verse_mapping: {
'8': '41-75',
'9': '1-92',
},
},
{
id: 11,
juz_number: 11,
verse_mapping: {
'9': '93-129',
'10': '1-109',
'11': '1-5',
},
},
{
id: 12,
juz_number: 12,
verse_mapping: {
'11': '6-123',
'12': '1-52',
},
},
{
id: 13,
juz_number: 13,
verse_mapping: {
'12': '53-111',
'13': '1-43',
'14': '1-52',
},
},
{
id: 14,
juz_number: 14,
verse_mapping: {
'15': '1-99',
'16': '1-128',
},
},
{
id: 15,
juz_number: 15,
verse_mapping: {
'17': '1-111',
'18': '1-74',
},
},
{
id: 16,
juz_number: 16,
verse_mapping: {
'18': '75-110',
'19': '1-98',
'20': '1-135',
},
},
{
id: 17,
juz_number: 17,
verse_mapping: {
'21': '1-112',
'22': '1-78',
},
},
{
id: 18,
juz_number: 18,
verse_mapping: {
'23': '1-118',
'24': '1-64',
'25': '1-20',
},
},
{
id: 19,
juz_number: 19,
verse_mapping: {
'25': '21-77',
'26': '1-227',
'27': '1-55',
},
},
{
id: 20,
juz_number: 20,
verse_mapping: {
'27': '56-93',
'28': '1-88',
'29': '1-45',
},
},
{
id: 21,
juz_number: 21,
verse_mapping: {
'29': '46-69',
'30': '1-60',
'31': '1-34',
'32': '1-30',
'33': '1-30',
},
},
{
id: 22,
juz_number: 22,
verse_mapping: {
'33': '31-73',
'34': '1-54',
'35': '1-45',
'36': '1-27',
},
},
{
id: 24,
juz_number: 24,
verse_mapping: {
'39': '32-75',
'40': '1-85',
'41': '1-46',
},
},
{
id: 23,
juz_number: 23,
verse_mapping: {
'36': '28-83',
'37': '1-182',
'38': '1-88',
'39': '1-31',
},
},
{
id: 25,
juz_number: 25,
verse_mapping: {
'41': '47-54',
'42': '1-53',
'43': '1-89',
'44': '1-59',
'45': '1-37',
},
},
{
id: 26,
juz_number: 26,
verse_mapping: {
'46': '1-35',
'47': '1-38',
'48': '1-29',
'49': '1-18',
'50': '1-45',
'51': '1-30',
},
},
{
id: 27,
juz_number: 27,
verse_mapping: {
'51': '31-60',
'52': '1-49',
'53': '1-62',
'54': '1-55',
'55': '1-78',
'56': '1-96',
'57': '1-29',
},
},
{
id: 28,
juz_number: 28,
verse_mapping: {
'58': '1-22',
'59': '1-24',
'60': '1-13',
'61': '1-14',
'62': '1-11',
'63': '1-11',
'64': '1-18',
'65': '1-12',
'66': '1-12',
},
},
{
id: 29,
juz_number: 29,
verse_mapping: {
'67': '1-30',
'68': '1-52',
'69': '1-52',
'70': '1-44',
'71': '1-28',
'72': '1-28',
'73': '1-20',
'74': '1-56',
'75': '1-40',
'76': '1-31',
'77': '1-50',
},
},
{
id: 30,
juz_number: 30,
verse_mapping: {
'78': '1-40',
'79': '1-46',
'80': '1-42',
'81': '1-29',
'82': '1-19',
'83': '1-36',
'84': '1-25',
'85': '1-22',
'86': '1-17',
'87': '1-19',
'88': '1-26',
'89': '1-30',
'90': '1-20',
'91': '1-15',
'92': '1-21',
'93': '1-11',
'94': '1-8',
'95': '1-8',
'96': '1-19',
'97': '1-5',
'98': '1-8',
'99': '1-8',
'100': '1-11',
'101': '1-11',
'102': '1-8',
'103': '1-3',
'104': '1-9',
'105': '1-5',
'106': '1-4',
'107': '1-7',
'108': '1-3',
'109': '1-6',
'110': '1-3',
'111': '1-5',
'112': '1-4',
'113': '1-5',
'114': '1-6',
},
},
],
juzByVerseTotalNumber: [
{ juz: 1, chapter_in_juz: ['1', '2'] },
{ juz: 2, chapter_in_juz: ['2'] },
{ juz: 3, chapter_in_juz: ['2', '3'] },
{ juz: 4, chapter_in_juz: ['3', '4'] },
{ juz: 5, chapter_in_juz: ['4'] },
{ juz: 6, chapter_in_juz: ['4', '5'] },
{ juz: 7, chapter_in_juz: ['5', '6'] },
{ juz: 8, chapter_in_juz: ['6', '7'] },
{ juz: 9, chapter_in_juz: ['7', '8'] },
{ juz: 10, chapter_in_juz: ['8', '9'] },
{ juz: 11, chapter_in_juz: ['9', '10', '11'] },
{ juz: 12, chapter_in_juz: ['11', '12'] },
{ juz: 13, chapter_in_juz: ['12', '13', '14'] },
{ juz: 14, chapter_in_juz: ['15', '16'] },
{ juz: 15, chapter_in_juz: ['17', '18'] },
{ juz: 16, chapter_in_juz: ['18', '19', '20'] },
{ juz: 17, chapter_in_juz: ['21', '22'] },
{ juz: 18, chapter_in_juz: ['23', '24', '25'] },
{ juz: 19, chapter_in_juz: ['25', '26', '27'] },
{ juz: 20, chapter_in_juz: ['27', '28', '29'] },
{ juz: 21, chapter_in_juz: ['29', '30', '31', '32', '33'] },
{ juz: 22, chapter_in_juz: ['33', '34', '35', '36'] },
{ juz: 23, chapter_in_juz: ['39', '40', '41'] },
{ juz: 24, chapter_in_juz: ['36', '37', '38', '39'] },
{ juz: 25, chapter_in_juz: ['41', '42', '43', '44', '45'] },
{ juz: 26, chapter_in_juz: ['46', '47', '48', '49', '50', '51'] },
{ juz: 27, chapter_in_juz: ['51', '52', '53', '54', '55', '56', '57'] },
{ juz: 28, chapter_in_juz: ['58', '59', '60', '61', '62', '63', '64', '65', '66'] },
{ juz: 29, chapter_in_juz: ['67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77'] },
{
juz: 30,
chapter_in_juz: [
'78',
'79',
'80',
'81',
'82',
'83',
'84',
'85',
'86',
'87',
'88',
'89',
'90',
'91',
'92',
'93',
'94',
'95',
'96',
'97',
'98',
'99',
'100',
'101',
'102',
'103',
'104',
'105',
'106',
'107',
'108',
'109',
'110',
'111',
'112',
'113',
'114',
],
},
],
}
juzMemory() {
// @saveroo Dirty Approach
// Regarding handling Mapped process, from memorization[{chapter[]}] to juz[memorization[chapter{verses[]}]
// Problem:
// -memorization[chapter{}] and juz data are separated,
// Expectation:
// -Wanted to display and order by the juz, but not all the memorized verse,
// -if condition of memorized verses is unmet the criteria of juz, it wont be displayed.
// as example we have memorization at Chapter 1:1-7, chapter 2:1-2, chapter 55:10
// [Juz 1] << DISPLAYED
// [Memorization]
// [Chapter 1] 1:1-7
// [Chapter 2] 1:1-141
// [Juz 2] << NOT DISPLAYED
// [Memorization]
// [Chapter]
// [Juz 27] << DISPLAYED
// [Memorization]
// [Chapter 55] 10 (Our Memorized verses equal inRange between the constraint to be considered as juz 27)
// @2019
// You dont need Lodash
const inRange = (num, a, b = 0) => Math.min(a, b) <= num && num < Math.max(a, b);
// To reduce typing overhead, i put it thereeee, immutable const
const jz = this.juzByVerseTotalNumber;
const jm = this.juzMapping;
const mem = this.memorizations;
const memmap = mem.map(i => i.chapter);
// TODO: should have better approach
// This is the main array to contains the transformed 30juz.
let arr = [];
// So i dont forget how to iterate thru it...
// 1. first iterate the juz Array from 1 to 30
for (let i = 0; i < jz.length; i++) {
// Initialize empty array to hold morphed Juz array from 1 to 30
// This array after iteration will reset. to contain new item in 2nd iteration.
let arrChap = [];
// The object to be pushed thru
let objUpper = {
juz: jz[i].juz,
juz_start_at_verse: this.ajza[i + 1],
};
// 2. Iterate thru the chapter, in 30 iteration there is 7 iteration, to push, if chapter is included in the current juz
for (let j = 0; j < memmap.length; j++) {
if (jm[i].verse_mapping[`${memmap[j].chapter_number}`]) {
let jmap = jm[i].verse_mapping[`${memmap[j].chapter_number}`];
let splt = jmap.split('-');
if (inRange(mem[j].verses.length, splt[0], splt[1] + 1)) arrChap.push(mem[j]);
}
// ## Uncomment if chapter want to be included in juz, regardless condition of memorized verse is unmet
// If the current mapped chapter in memorization included in 30 JUZ iteration,
// then push to array which reseted in every new iteration
// if (jz[i].chapter_in_juz.includes(`${memmap[j].chapter_number}`)) {
//
// // push memorization which contain a chapter, since chapter has one-to-one relationship.
// // arrChap.push(mem[j]);
// }
}
// After done the 2nd iteration, then added props to Object which contain juz number and chapter array,
// Then push to main array
objUpper.chapter = arrChap;
arr.push(objUpper);
}
// Returned filter to remove empty array
return arr.filter(i => i.chapter.length !== 0);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment