From ace22b3fe1e8c6b2449f61bc356a998609295bde Mon Sep 17 00:00:00 2001 From: tejumola Date: Wed, 14 Aug 2019 11:44:06 +0100 Subject: [PATCH] ft(readtime):implement helper function to calculate read time [#166840894 Finishes] --- __test__/utils/index.spec.js | 10 ++++++++++ src/utils/index.js | 11 +++++++++++ 2 files changed, 21 insertions(+) create mode 100644 __test__/utils/index.spec.js create mode 100644 src/utils/index.js diff --git a/__test__/utils/index.spec.js b/__test__/utils/index.spec.js new file mode 100644 index 0000000..70cfc80 --- /dev/null +++ b/__test__/utils/index.spec.js @@ -0,0 +1,10 @@ +import calculateReadTime from '../../src/utils'; + +const content = + 'If you own a business in Lagos, or are trying to set one up, the chances are that someone has already told you that you are mad. I am here to confirm what you were told: yes you are mad. You are either currently mad, or will be in the future. This is what running a business in Lagos does to you Surely there has to be a better way? Yes! You could postpone your Estimated Touch Day (ETD — the day your head finally touches and you go coocoo), or reduce the effects of an already messed up mental condition. Either way, your family needs you, so it’s important to learn how to stay sane in Lagos, while building your business. This is the king of all business advices — but especially in Lagos. Credit is super expensive, so it’s very important to maintain some liquidity for the sake of your sanity. Put away a part of your receivables to form a rainy day fund for the business. Your rainy day fund should be about the amount needed to fund one or two payroll cycles.Not the government, not the tax man, not the auditor, not the client. The guys listed are only the official ones — your success or failures might depend on ensuring some people who have never been mentioned in a business book are happy. Think of all these guys whenever you’re running your numbers. That’s how you ensure none of them stiffs you. Actually, you’ll only manage damage limitation, because they will stiff you. In my last job, I used to oversee imports and distribution, and every time our containers came in, we had to pay off police, area boys, guys on the street, local chiefs etc. Every Christmas, the chief of the area boys would come over, sit with my boss for drinks, while we put together a Christmas package for him.'; + +describe('it should test calculate read time helper function', () => { + it('it should test calculate read time', () => { + expect(calculateReadTime(content)).toEqual('~2 min read'); + }); +}); diff --git a/src/utils/index.js b/src/utils/index.js new file mode 100644 index 0000000..09c342b --- /dev/null +++ b/src/utils/index.js @@ -0,0 +1,11 @@ +const calculateReadTime = content => { + const wordsPerMinute = 200; + + const textLength = content.split(' ').length; + if (textLength > wordsPerMinute) { + const value = Math.ceil(textLength / wordsPerMinute); + return `~${value} min read`; + } + return 'less than a min read'; +}; +export default calculateReadTime;