Skip to content

Instantly share code, notes, and snippets.

@bcls
bcls / isdefined.js
Created August 23, 2017 19:45
isdefined() #javascript
/**
* tests for all the ways a variable might be undefined or not have a value
* @param {*} x the variable to test
* @return {Boolean} true if variable is defined and has a value
*/
function isDefined(x) {
if ( x === '' || x === null || x === undefined) {
return false;
}
return true;
@bcls
bcls / restapi.js
Last active November 13, 2017 16:19
REST API app stem #javascript #codesamples
/**
* createRequest sets up requests, send them to makeRequest(), and handles responses
* @param {string} type the request type
*/
function createRequest(type) {
var options = {},
requestBody = {},
proxyURL = 'https:/solutions.brightcove.com/bcls/bcls-proxy/bcls-proxy.php'
ipBaseURL = 'https://ingestion.api.brightcove.com/v1/accounts/' + account.value,
diBaseURL = 'https://ingest.api.brightcove.com/v1/accounts/' + account.value,
@bcls
bcls / isjson.js
Last active September 27, 2017 20:57
is this string JSON? #javascript
/*
* tests to see if a string is json
* @param {String} str string to test
* @return {Boolean}
*/
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
@bcls
bcls / add-items-to-drupal-menu.js
Last active September 27, 2017 20:56
Add items to Drupal left (in-page) menu #drupal #javascript
// create navigation for page sections
function createInPageNavMenu() {
var sideNavList = document.querySelector('.bc-ipnav-block ul'),
// get reference to Related Topics item
lastLI = sideNavList.lastChild,
i,
max = navLabel.length,
aEl,
liEl,
txt;
@bcls
bcls / Remove_spaces_from_a_string.js
Last active September 27, 2017 20:56
Remove spaces from a string #javascript
/**
* remove spaces from a string
* @param {String} str string to process
* @return {String} trimmed string
*/
function removeSpaces(str) {
str= str.replace(/\s/g, '');
return str;
}
@bcls
bcls / get_URL_param_value.js
Last active September 27, 2017 20:55
get URL param value #javascript
/**
* gets value of a URL param if exists
* @param {string} name the param you want the value of
* @return {string} result value of param if exists or ""
*/
function getURLparam(name) {
var regex,
results;
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
@bcls
bcls / append_new_content_to_HTML.js
Last active September 27, 2017 20:55
append new content to HTML #javascript
/**
* append new HTML elements to some existing one - efficiently
* @param {Object} htmlEl reference to the HTML element to append new HTML to
*/
function appendHTML(htmlEl) {
var i = 0, el, fragment = document.createDocumentFragment();
while (i < 200) {
el = document.createElement('li');
el.innerText = 'This is my list item number ' + i;
@bcls
bcls / PHP_PUT_file.php
Last active May 28, 2023 20:10
PHP PUT file #php
$file = fopen($file_on_dir_not_url, "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
@bcls
bcls / convert_milliseconds_to_formatted_time.php
Last active August 10, 2023 12:10
convert milliseconds to formatted time #php
/**
* Converts milliseconds to formatted time or seconds.
* @param int [$ms] The length of the media asset in milliseconds
* @param bool [$seconds] Whether to return only seconds
* @return mixed The formatted length or total seconds of the media asset
*/
function convertTime($ms, $seconds = false)
{
$total_seconds = ($ms / 1000);
@bcls
bcls / seconds_to_formatted_time.js
Last active September 27, 2017 20:54
seconds to formatted time #javascript
/**
* utility to extract h/m/s from seconds
* @param {number} secs - seconds to convert to hh:mm:ss
* @returns {object} object with members h (hours), m (minutes), s (seconds)
*/
function secondsToTime(secs) {
var hours = Math.floor(secs / (60 * 60)),
divisor_for_minutes = secs % (60 * 60),
minutes = Math.floor(divisor_for_minutes / 60),
divisor_for_seconds = divisor_for_minutes % 60,