Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. app.filter('partition', function() {
  2. //cache for non subsequent calls
  3. var cache = {};
  4.  
  5. var filter = function(arg, len) {
  6.  
  7. if(!arg) { return;}
  8.  
  9. var chunks = [];
  10.  
  11. // create desired number of empty lists
  12. for(var i=0; i<len; i++){
  13. chunks.push([]);
  14. }
  15. // iterate over elements
  16. for(var j=0; j<arg.length ;j++)
  17. {
  18. chunks[j%len].push(arg[j]);
  19. }
  20.  
  21. //cache for non subsequent calls
  22. var argString = JSON.stringify(arg);
  23. var fromCache = cache[argString+len];
  24. if (JSON.stringify(fromCache) === JSON.stringify(chunks)) {
  25. return fromCache;
  26. }
  27. cache[argString+len] = chunks;
  28.  
  29. return chunks;
  30. };
  31.  
  32. return filter;
  33. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement