Skip to content

Commit

Permalink
Refactor description
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jul 1, 2017
1 parent 760b503 commit 2d1ae48
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
32 changes: 21 additions & 11 deletions src/rules/description.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
'use strict'

const condenseWhitespace = require('condense-whitespace')
const isString = require('lodash.isstring')
const flow = require('lodash.flow')

const REGEX_LOCATION = /^[A-Z\s]+\s+[-—–]\s+/

const removeLocation = value => value.replace(REGEX_LOCATION, '')

const sanetize = flow([
// trim extra whitespace
condenseWhitespace,
// if it starts with a location, like articles sometimes do in the opening
// paragraph, try to remove it
removeLocation
])

/**
* Wrap a rule with validation and formatting logic.
Expand All @@ -6,19 +23,12 @@
* @return {Function} wrapped
*/

function wrap(rule) {
function wrap (rule) {
return ($) => {
let value = rule($)
if (typeof value != 'string') return

// remove extra whitespace
value = value.trim()

// if it starts with a location, like articles sometimes do in the opening
// paragraph, try to remove it
value = value.replace(/^[A-Z\s]+\s+[-—–]\s+/, '')

return value
if (!isString(value)) return
return sanetize(value)
}
}

Expand All @@ -34,5 +44,5 @@ module.exports = [
wrap(($) => $('meta[itemprop="description"]').attr('content')),
wrap(($) => $('.post-content p').first().text()),
wrap(($) => $('.entry-content p').first().text()),
wrap(($) => $('article p').first().text()),
wrap(($) => $('article p').first().text())
]
2 changes: 1 addition & 1 deletion test/fixtures/linkedin/output.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Ramya Joseph",
"date": "2016-05-26T00:00:00.000Z",
"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,",
"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,",
"image": "https://media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAdIAAAAJDBkYThmMTYyLWY2NjUtNGU4Ny1iOTBhLWE1NzU5Y2Y1NzgxNA.jpg",
"publisher": "LinkedIn Pulse",
"title": "How Can AI Change Financial Services? Consolidation of the #FinAI tweetstorm",
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/san-francisco-chronicle/output.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Carolyn Said",
"date": "2016-04-25T23:37:00.000Z",
"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.",
"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.",
"image": "http://ww3.hdnux.com/photos/45/53/13/9876166/5/rawImage.jpg",
"publisher": "San Francisco Chronicle",
"title": "Nasdaq center in SF offers free classes for entrepreneurs",
Expand Down

0 comments on commit 2d1ae48

Please sign in to comment.