PHP Snippet – array_splice_assoc(…)

Recently I was working on something that required inserting key-value pairs into an array after a specific key in PHP. As I was looking through stackoverflow answers I realized that there was no awesome answer that really jumped out at me. Therefore I decided to take a few minutes to just write my own array_splice_assoc() function to get the job done:

As you can see this function takes the same parameters that array_splice() does but the difference is that the offset can be a string in this key which refers to the key after which the new array elements should be inserted.

One important thing to note is that if you try to insert a key-value pair in which the key exists before or at the offset, your key-value pair will not be inserted. On the other hand if you try to insert a key-value pair in which the key exists after offset, your value will replace the previous one.

Have fun! 😎

JavaScript – Faking Native Functions

The other day I kind of just wanted a reason to write a Gist and embed it on my site using this method. What I came up with was a quick way to make all functions appear to be native:
https://gist.github.com/9240810

Here is an example of what happens:

function alertAndReturn() {
  alert.apply(this, arguments);
  return arguments;
}

alert(alertAndReturn); // function alertAndReturn() { [native code] }
alert(function(){});   // function () { [native code] }

I have no clue when this would be useful in the real world but if you need it, here it is! 😎

Git – Bash Command-line Aliases

Just as at many other companies, at Monetate we use Github as our source control system. Recently I decided to dedicate some time to create some helpful aliases to save some time. Assuming that you create new branches all based off of your “master” branch these aliases may be helpful for you as well:

[alias]
	# Show a log of all commits with any pertinent files listed below each
	# commit.
	lo = "log --name-only --color --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

	# Creates a new branch and checks it out.
	b-co = checkout -b

	# Remove the specified local branch.
	b-rm = branch --delete

	# Remove the specified remote branch.
	rb-rm = push --delete origin

	# A diff showing what has been added or taken away in this branch in
	# comparison to what is in master.
	b-diff = "! git diff 'HEAD~'$(git log HEAD ^master --oneline | wc -l | tr -d ' ') --color"

	# Show a log of all commits found in this branch but not in master.
	b-log = log master..HEAD --color --name-only

	# Rebase this branch, allowing for all commits added in this branch to be
	# modified.
	b-rebase = "! git rebase -i 'HEAD~'$(git log HEAD ^master --oneline | wc -l | tr -d ' ')"

	# Opens a new window in Sublime Text 2 with all the files that have been
	# modified in this branch.
	b-subl = "! cd $(git rev-parse --show-toplevel) && /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl --new-window $(git b-diff --name-status | awk '/^[^D][ \t]+(.+)/ {print $2}')"

All of the above aliases can be added to your .gitconfig file which is probably located at “~/.gitconfig“. The following explains what adding each of the above aliases actually does:

Command Description
git lo Similar to a git log but instead of printing each commit in its own block, each commit only shows on one line: efce549 - Name of the Commit (28 hours ago) <John Doe>
git b-co <new_branch_name> Creates the branch specified and then checks it out.
git b-rm <branch_name> Removes the specified local branch. In order to force the removal of the branch you can use git b-rm -f <branch_name>.
git rb-rm <remote_branch_name> Removes the specified remote branch. In order to force the removal of the remote branch you can use git rb-rm -f <branch_name>.
git b-diff Does a git diff HEAD~# where # is the number of commits found in this branch which are not found in your local master.
git b-log Shows the logs for all of the commits found in this branch which aren’t in the master branch.
git b-rebase Does a git rebase -i HEAD~# where # is the number of commits found in this branch which are not found in your local master. Especially useful for squashing all of your commits into one.
git b-subl Opens all of the files which have been updated in this branch in Sublime Text 2 (in comparison with what is in your local master). NOTE: This may need to be modified if Sublime Text 2 is not located at /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl.

It is important to note that you can change the “Sublime Text 2” shortcut to the editor of your choice. I just put that there to show you how to get all files that were updated in this branch to open. Have fun, and let us know if you have any modifications or additional aliases! 8)