Skip to content

Commit 2d1ae48

Browse files
committed
Refactor description
1 parent 760b503 commit 2d1ae48

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/rules/description.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
'use strict'
2+
3+
const condenseWhitespace = require('condense-whitespace')
4+
const isString = require('lodash.isstring')
5+
const flow = require('lodash.flow')
6+
7+
const REGEX_LOCATION = /^[A-Z\s]+\s+[-]\s+/
8+
9+
const removeLocation = value => value.replace(REGEX_LOCATION, '')
10+
11+
const sanetize = flow([
12+
// trim extra whitespace
13+
condenseWhitespace,
14+
// if it starts with a location, like articles sometimes do in the opening
15+
// paragraph, try to remove it
16+
removeLocation
17+
])
118

219
/**
320
* Wrap a rule with validation and formatting logic.
@@ -6,19 +23,12 @@
623
* @return {Function} wrapped
724
*/
825

9-
function wrap(rule) {
26+
function wrap (rule) {
1027
return ($) => {
1128
let value = rule($)
12-
if (typeof value != 'string') return
13-
14-
// remove extra whitespace
15-
value = value.trim()
16-
17-
// if it starts with a location, like articles sometimes do in the opening
18-
// paragraph, try to remove it
19-
value = value.replace(/^[A-Z\s]+\s+[-]\s+/, '')
2029

21-
return value
30+
if (!isString(value)) return
31+
return sanetize(value)
2232
}
2333
}
2434

@@ -34,5 +44,5 @@ module.exports = [
3444
wrap(($) => $('meta[itemprop="description"]').attr('content')),
3545
wrap(($) => $('.post-content p').first().text()),
3646
wrap(($) => $('.entry-content p').first().text()),
37-
wrap(($) => $('article p').first().text()),
47+
wrap(($) => $('article p').first().text())
3848
]

test/fixtures/linkedin/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Ramya Joseph",
33
"date": "2016-05-26T00:00:00.000Z",
4-
"description": "We had a tweetstorm today on #FinAI and the role AI will play to change financial services. Here is the consolidation of tweets. 1/ Fintech in 2016 is similar to the Internet in 1996 – Yahoo,",
4+
"description": "We had a tweetstorm today on #FinAI and the role AI will play to change financial services. Here is the consolidation of tweets. 1/ Fintech in 2016 is similar to the Internet in 1996 – Yahoo,",
55
"image": "https://media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAdIAAAAJDBkYThmMTYyLWY2NjUtNGU4Ny1iOTBhLWE1NzU5Y2Y1NzgxNA.jpg",
66
"publisher": "LinkedIn Pulse",
77
"title": "How Can AI Change Financial Services? Consolidation of the #FinAI tweetstorm",

test/fixtures/san-francisco-chronicle/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Carolyn Said",
33
"date": "2016-04-25T23:37:00.000Z",
4-
"description": "[...] it’s natural to think that San Francisco’s new Nasdaq Entrepreneurial Center, which sprawls over 13,000 square feet in a prime SoMa location, would cater exclusively to up-and-coming tech startups. [...] all kind of entrepreneurs, whether they’re launching a taco truck or the next Google, can plug into the center for free classes, mentorship, media coaching, meeting space and other support. The nonprofit center, located near the future Transbay Transit Center, is backed by $10 million from the Nasdaq Educational Foundation, the stock exchange’s 22-year-old philanthropy arm. Outside 505 Howard St., a Times-Square-style electronic ticker scrolls promotions for events and logos of corporate sponsors. Inside, the expansive ground-floor space is divided into various functional areas: a state-of-the-art broadcast studio where budding Jack Dorseys or Marissa Mayers can learn how to handle on-camera interviews; classrooms that can accommodate dozens of people for hands-on trainings; a cafe and meeting space for collaborations; and a big lobby that serves double duty as space for pop-up shops to test products and ideas. The recent college grad knocked on the door, showed off his fledgling product-design studio’s first product, a wristwatch, and was invited to have a three-week pop-up store on site. “It was the absolute best way I could have launched my brand,” said Umaña, whose company, Classic Engineering, is now working on a sculpture-like lamp that turns on and off in response to whistling. There are deep dives into topics like company formation, business mechanics, understanding intellectual property and data security, among others. “Building buzz and excitement around your brand is really an art,” said Alexa Fleischman, CEO and co-founder of Savvy Society, which lets girls design and purchase custom 3-D-printed fashion accessories. [...] that venture funding is tighter, it will offer tips on how to weather this period with alternative financing strategies, for instance. The center hosted a two-day regional workshop for 1776’s Challenge Cup startup contest and helped it identify interesting entrepreneurs, mentors, coaches and local experts.",
4+
"description": "[...] it’s natural to think that San Francisco’s new Nasdaq Entrepreneurial Center, which sprawls over 13,000 square feet in a prime SoMa location, would cater exclusively to up-and-coming tech startups. [...] all kind of entrepreneurs, whether they’re launching a taco truck or the next Google, can plug into the center for free classes, mentorship, media coaching, meeting space and other support. The nonprofit center, located near the future Transbay Transit Center, is backed by $10 million from the Nasdaq Educational Foundation, the stock exchange’s 22-year-old philanthropy arm. Outside 505 Howard St., a Times-Square-style electronic ticker scrolls promotions for events and logos of corporate sponsors. Inside, the expansive ground-floor space is divided into various functional areas: a state-of-the-art broadcast studio where budding Jack Dorseys or Marissa Mayers can learn how to handle on-camera interviews; classrooms that can accommodate dozens of people for hands-on trainings; a cafe and meeting space for collaborations; and a big lobby that serves double duty as space for pop-up shops to test products and ideas. The recent college grad knocked on the door, showed off his fledgling product-design studio’s first product, a wristwatch, and was invited to have a three-week pop-up store on site. “It was the absolute best way I could have launched my brand,” said Umaña, whose company, Classic Engineering, is now working on a sculpture-like lamp that turns on and off in response to whistling. There are deep dives into topics like company formation, business mechanics, understanding intellectual property and data security, among others. “Building buzz and excitement around your brand is really an art,” said Alexa Fleischman, CEO and co-founder of Savvy Society, which lets girls design and purchase custom 3-D-printed fashion accessories. [...] that venture funding is tighter, it will offer tips on how to weather this period with alternative financing strategies, for instance. The center hosted a two-day regional workshop for 1776’s Challenge Cup startup contest and helped it identify interesting entrepreneurs, mentors, coaches and local experts.",
55
"image": "http://ww3.hdnux.com/photos/45/53/13/9876166/5/rawImage.jpg",
66
"publisher": "San Francisco Chronicle",
77
"title": "Nasdaq center in SF offers free classes for entrepreneurs",

0 commit comments

Comments
 (0)