Have a look at the Contract Killer contract, it has a provision on Intellectual Property which would be useful to you.
Intellectual property rights
You guarantee the written content you provide is original, or that you have the rights to use it. You also guarantee you have licences to use images which are owned by photographers or have been purchased from stock libraries. You agree to protect us from any claim by owners of copyrighted material. When our work requires licensed fonts or images from stock libraries, we’ll ask you to purchase them so you’ll be licensed to use them.We guarantee our work is original. When you’ve paid us in full—and if this contract hasn’t been terminated—we’ll assign intellectual property rights to you as follows:
You’ll own the graphic files we produce during your project. We’ll give you permission to use these files for any purpose.
We’ll own the unique combination of elements which constitute the complete design. We’ll license it to you, exclusively and in perpetuity, for this project only.
This should technically protect you and your code from reuse for purposes other than the intended project.
A previous version of this contract had text that worked better in my opinion.
Intellectual property rights
Just to be clear, “Intellectual property rights” means all patents, rights to inventions, copyright (including rights in software) and related rights, trademarks, service marks, get up and trade names, internet domain names, rights to goodwill or to sue for passing off, rights in designs, database rights, rights in confidential information (including know-how) and any other intellectual property rights, in each case whether registered or unregistered and including all applications (or rights to apply) for, and renewals or extensions of, such rights and all similar or equivalent rights or forms of protection which subsist or shall subsist now or in the future in any part of the world.
First, you guarantee that all elements of text, images or other artwork you provide are either owned by your good selves, or that you’ve permission to use them. When you provide text, images or other artwork to us, you agree to protect us from any claim by a third party that we’re using their intellectual property.
We guarantee that all elements of the work we deliver to you are either owned by us or we’ve obtained permission to provide them to you. When we provide text, images or other artwork to you, we agree to protect you from any claim by a third party that you’re using their intellectual property. Provided you’ve paid for the work and that this contract hasn’t been terminated, we’ll assign all intellectual property rights to you as follows:
You’ll own the website we design for you plus the visual elements that we create for it. We’ll give you source files and finished files and you should keep them somewhere safe as we’re not required to keep a copy. You own all intellectual property rights of text, images, site specification and data you provided, unless someone else owns them.
We’ll own any intellectual property rights we’ve developed prior to, or developed separately from this project and not paid for by you. We’ll own the unique combination of these elements that constitutes a complete design and we’ll license its use to you, exclusively and in perpetuity for this project only, unless we agree otherwise.
In the case of the above, your boiler code is developed prior to the development of the project, as such you only license the use of such code, and the code cannot be reused by the client without your written permission.
Regardless, I would always recommend you consult a lawyer who would be able to give you better advice.
Defining scope of what's yours or what's theirs
In your particular case, I would convert your code and functions into a library, after which you could license the use of that library instead of embedding your code into the product. My knowledge about programming doesn't go beyond Web Development so if it were me, and I had some JavaScript functions that were unique to my work, I would bundle it up in a file or folder and license it exclusively to that project.