-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const MATCH_START_QUERY = /[\?&#](?:start|t)=([0-9hms]+)/ | ||
const MATCH_START_STAMP = /(\d+)(h|m|s)/g | ||
const MATCH_NUMERIC = /^\d+$/ | ||
|
||
// Parse YouTube URL for a start time param, ie ?t=1h14m30s | ||
// and return the start time in seconds | ||
export function parseStartTime (url) { | ||
const match = url.match(MATCH_START_QUERY) | ||
if (match) { | ||
const stamp = match[1] | ||
if (stamp.match(MATCH_START_STAMP)) { | ||
return parseStartStamp(stamp) | ||
} | ||
if (MATCH_NUMERIC.test(stamp)) { | ||
return parseInt(stamp, 10) | ||
} | ||
} | ||
return 0 | ||
} | ||
|
||
function parseStartStamp (stamp) { | ||
let seconds = 0 | ||
let array = MATCH_START_STAMP.exec(stamp) | ||
while (array !== null) { | ||
const [, count, period] = array | ||
if (period === 'h') seconds += parseInt(count, 10) * 60 * 60 | ||
if (period === 'm') seconds += parseInt(count, 10) * 60 | ||
if (period === 's') seconds += parseInt(count, 10) | ||
array = MATCH_START_STAMP.exec(stamp) | ||
} | ||
return seconds | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { describe, it } from 'mocha' | ||
import { expect } from 'chai' | ||
|
||
import { parseStartTime } from '../../src/utils' | ||
|
||
const YOUTUBE_URL = 'http://youtu.be/12345678901' | ||
|
||
describe('parseStartTime', () => { | ||
it('parses seconds', () => { | ||
expect(parseStartTime(YOUTUBE_URL + '?start=162')).to.equal(162) | ||
}) | ||
|
||
it('parses stamps', () => { | ||
expect(parseStartTime(YOUTUBE_URL + '?start=48s')).to.equal(48) | ||
expect(parseStartTime(YOUTUBE_URL + '?start=3m15s')).to.equal(195) | ||
expect(parseStartTime(YOUTUBE_URL + '?start=1h36m17s')).to.equal(5777) | ||
}) | ||
|
||
it('parses with other params', () => { | ||
expect(parseStartTime(YOUTUBE_URL + '?param=1&start=32')).to.equal(32) | ||
}) | ||
|
||
it('parses using t', () => { | ||
expect(parseStartTime(YOUTUBE_URL + '?t=32')).to.equal(32) | ||
}) | ||
|
||
it('parses using a hash', () => { | ||
expect(parseStartTime(YOUTUBE_URL + '#t=32')).to.equal(32) | ||
expect(parseStartTime(YOUTUBE_URL + '#start=32')).to.equal(32) | ||
}) | ||
|
||
it('parses using a hash', () => { | ||
expect(parseStartTime(YOUTUBE_URL + '#t=32')).to.equal(32) | ||
expect(parseStartTime(YOUTUBE_URL + '#start=32')).to.equal(32) | ||
}) | ||
|
||
it('returns 0 for invalid stamps', () => { | ||
expect(parseStartTime(YOUTUBE_URL)).to.equal(0) | ||
expect(parseStartTime(YOUTUBE_URL + '?start=')).to.equal(0) | ||
expect(parseStartTime(YOUTUBE_URL + '?start=hms')).to.equal(0) | ||
expect(parseStartTime(YOUTUBE_URL + '?start=invalid')).to.equal(0) | ||
expect(parseStartTime(YOUTUBE_URL + '?strat=32')).to.equal(0) | ||
expect(parseStartTime(YOUTUBE_URL + '#s=32')).to.equal(0) | ||
}) | ||
}) |