Skip to content

Instantly share code, notes, and snippets.

@reggi
Last active November 6, 2015 22:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reggi/5831361127099bfc0f1d to your computer and use it in GitHub Desktop.
Save reggi/5831361127099bfc0f1d to your computer and use it in GitHub Desktop.

Search for functions via expected imput / output

I have a programming need. I need a function that most-likley exists already. Someone has made it and it's probably public. If it doesn't exist theres probably some combination of functions out there that exist that can make it happen.

Here's a string input and output.

'focus together' > 'focus_together'
'focusTogether' > 'focus_together'
'focus Together' > 'focus_together'
'focus. Together' > 'focus_together'
'focus, Together' > 'focus_together'
'focus, Together!' > 'focus_together'
'focus,Together!' > 'focus_together'
'focus,tOgether!' > 'focus_together'

I need a way to search all of the web for a function that will transform my string with this above example as input or query.

I know from my journey's around the web that there are a couple of things that do this. I know that shopify has a handelize liquid filter does something very similar. There's an npm module underscore.string with a underscored function that does something almost exactly what I need (it doesn't remove punctuation). Without doing a ton of research and knowing what's out there, there's no real way to know if your spending your time doing something valuable or wasting it writing functions and tests for those functions that already exist.

There is no good way to name complicated simple functions that are searchable. Functions can't be found based on their name, they have to be found based on what they do.

Winding back the way we do development is there a method where we can write code based on what we need and how to find it rather then coming up with our own solutions everytime? This idea takes test-driven-development TDD one step further, is there a way I can write tests (like the one above) which generate my functions using open source libraries behind the scenes?

The example above is a simple one, and can definatley be "catered to" I can make a function with a swtich that looks for those exact strings and outputs my desired result which wouldn't be what I want.

I know I can use regex to describe how I want all punctuation and white space to be squashed, and I can describe with code only how I want to split the string at any captial letters, when camel-cased. I can show what my desired results are, but there's a barrier to describing how things work dynamically without code. Regex isn't something a beginner isn't gonna just pickup, and even if your a seasoned programmer how do you know what the most efficient way to do the above operation is?

Right now most of the modules we use are black boxes. They do a specific thing and it's compartmentalized. I like the idea of many people competing for the most optimal way of doing these small things and what's happening under the hood is still a black box, it's just a much more ever-evolving one.

Here's a rough example, I know search would be async and also need to be cached somehow, but this is the general idea.

import { search } from 'blackbox'
let spec = `'focus together' > 'focus_together'
            'focusTogether' > 'focus_together'
            'focus Together' > 'focus_together'
            'focus. Together' > 'focus_together'
            'focus, Together' > 'focus_together'
            'focus, Together!' > 'focus_together'
            'focus,Together!' > 'focus_together'
            'focus,tOgether!' > 'focus_together'`
export default handleize = search(spec)
handleize('winnerWinner') // => winner_winner

I'm very inspired by sites like algorithmia, that black-box most of the functionalty of simple functions into an easy to use module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment