d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Javascript String Parsing Task > 100fg
Add Reply New Topic New Poll
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Dec 11 2019 09:07am


Convert the below string into a parsed string and an object depending on the function parameter mode === 'string' or mode === 'object.'

Code
let string = `Things I'm Never Doing Again: napping during construction work; riding bicycle in the rain. Fruits: mangoes; peaches; apples. Untagged singleton sentences go into the 'unsorted' category. Style Rules: Partitions are delineated by periods. Style Rules: Like-groupings are delineated by semicolons. Style Rules: Like groupings are merged into the same category.`


----------------------------------------------------------
If Partition Mode = 'String', then this is the output:
----------------------------------------------------------

Things I'm Never Doing Again:
Napping during construction work.
Riding bicycle in the rain.

Fruits:
Mangoes.
Peaches.
Apples.

Unsorted:
Untagged singleton sentences go into the 'unsorted' category.

Style Rules:
Partitions are delineated by periods.
Like-groupings are delineated by semicolons.
Like groupings are merged into the same category.

----------------------------------------------------------
If Partition Mode = 'Object', then this is the output:
----------------------------------------------------------

Code
{
"Things I'm Never Doing Again": [
'Napping during construction work.',
'Riding bicycle in the rain.'
],
"Fruits": [
'Mangoes.',
'Peaches.',
'Apples.',
],
"Unsorted": [
"Untagged singleton sentences go into the 'unsorted' category."
],
"Style Rules": [
"Partitions are delineated by periods.",
"Like-groupings are delineated by semicolons.",
"Like groupings are merged into the same category."
]
}
Member
Posts: 12,703
Joined: May 17 2013
Gold: 12,935.00
Dec 11 2019 02:33pm
Code
let string = `Things I'm Never Doing Again: napping during construction work; riding bicycle in the rain. Fruits: mangoes; peaches; apples. Untagged singleton sentences go into the 'unsorted' category. Style Rules: Partitions are delineated by periods. Style Rules: Like-groupings are delineated by semicolons. Style Rules: Like groupings are merged into the same category.`


var parseString = function(string, mode) {
let obj = {
'Unsorted': [],
};
let order = [];


string.split('.').map(group => {
let like = group.split(':');
if (like.length === 2) {
let key = like[0].trim()
key = key[0].toUpperCase() + key.slice(1);
let remaining = like[1].split(';');
if (order.indexOf(key) === -1) {
order.push(key);
obj[key] = remaining.map(value => {
value = value.trim();
if (value !== '') {
value = value[0].toUpperCase() + value.slice(1);
return value + '.';
}
})
} else {
obj[key].push(...remaining.map(value => {
value = value.trim();
if (value !== '') {
value = value[0].toUpperCase() + value.slice(1);
return value + '.';
}
}));
}
} else if (like.length === 1) {
group = group.trim();
if (group !== '') {
if (order.indexOf('Unsorted') === -1) {
order.push('Unsorted');
}
group = group[0].toUpperCase() + group.slice(1) + '.';
obj['Unsorted'].push(group);
}
}
})

if (mode === "object"){
return obj;
} else if (mode === "string") {
let values = order.map((value, index) => {
if (index !== order.length){
let retvalue = '';
retvalue = retvalue.concat(value, ':\n', obj[value].join('\n'))
return retvalue
}
})
return values.join('\n\n');
}
}


result of calling parseString:


This post was edited by Klexmoo on Dec 11 2019 02:36pm
Member
Posts: 14,925
Joined: Jan 3 2008
Gold: 135,879.75
Dec 11 2019 06:38pm
Quote (Klexmoo @ Dec 11 2019 03:33pm)
Code
let string = `Things I'm Never Doing Again: napping during construction work; riding bicycle in the rain. Fruits: mangoes; peaches; apples. Untagged singleton sentences go into the 'unsorted' category. Style Rules: Partitions are delineated by periods. Style Rules: Like-groupings are delineated by semicolons. Style Rules: Like groupings are merged into the same category.`


var parseString = function(string, mode) {
let obj = {
'Unsorted': [],
};
let order = [];


string.split('.').map(group => {
let like = group.split(':');
if (like.length === 2) {
let key = like[0].trim()
key = key[0].toUpperCase() + key.slice(1);
let remaining = like[1].split(';');
if (order.indexOf(key) === -1) {
order.push(key);
obj[key] = remaining.map(value => {
value = value.trim();
if (value !== '') {
value = value[0].toUpperCase() + value.slice(1);
return value + '.';
}
})
} else {
obj[key].push(...remaining.map(value => {
value = value.trim();
if (value !== '') {
value = value[0].toUpperCase() + value.slice(1);
return value + '.';
}
}));
}
} else if (like.length === 1) {
group = group.trim();
if (group !== '') {
if (order.indexOf('Unsorted') === -1) {
order.push('Unsorted');
}
group = group[0].toUpperCase() + group.slice(1) + '.';
obj['Unsorted'].push(group);
}
}
})

if (mode === "object"){
return obj;
} else if (mode === "string") {
let values = order.map((value, index) => {
if (index !== order.length){
let retvalue = '';
retvalue = retvalue.concat(value, ':\n', obj[value].join('\n'))
return retvalue
}
})
return values.join('\n\n');
}
}


result of calling parseString:
https://i.imgur.com/ewPCNt2.png


Beautiful stuff. Thanks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll