search javascript refactor