Avoid code duplication in js